画长方形 .cpp代码如下
- C/C++ code
#include <QtGui>#include <QPushButton>#include <QFont>#include <QSpinBox>#include <QSlider>#include <QHBoxLayout>#include <QWidget>#include <Qt>#include <QPainter>#include "test.h"Test::Test(QWidget *parent):QWidget(parent){ resize(800,600); setWindowTitle(QObject::tr("Paint Demo"));}void Test::paint(QPaintEvent *){ QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.drawLine( 80,100,100,100 ); painter.setPen( Qt::red ); painter.drawRect(10,10,100,400); painter.setPen( QPen(Qt::green,5)); painter.setBrush( Qt::blue ); painter.drawEllipse( 50,150,400,200 ); painter.save();}头文件代码如下:#ifndef TEST_H#define TEST_H#include <QtGui/QMainWindow>class Test:public QWidget{ Q_OBJECTpublic: Test(QWidget *parent = 0);protected:private: void paint(QPaintEvent *event);};#endifmain.cpp代码#include <QtGui/QApplication>#include "test.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); Test w; w.show(); return a.exec();}
我用的是LINUX下的QT creator
能运行,标题栏也改了,但是就是不更改颜色。用time也更新过,QOBJECT也试过注释,希望各位师哥师姐指点下啊。还有就是输出汉字的问题,这边运行tr的时候说是未声明.加上QOBJECT行了可是还是乱码, 能一并解决了么?在线等,谢谢
------解决方案--------------------
1.
- C/C++ code
//void paint(QPaintEvent *event);void paintEvent(QPaintEvent *event);
------解决方案--------------------
1,在paintEvent事件里执行
2,用QGraphicsView