#include <QApplication>#include "draw.h"int main(int argc, char *argv[]) { QApplication app(argc, argv); mainDialog *dialog = new mainDialog; dialog->show(); return app.exec();}
#ifndef DRAW_H #define DRAW_H#include <QDialog>#include <QPainter>class QFrame;class mainDialog : public QDialog { Q_OBJECT public: mainDialog(QWidget *parent = 0);// signals: // private slots: private://////// Frame //////// QFrame *paintFrame;//////// Painter //////// QPainter framePainter;};#endif
#include <QtGui> #include "draw.h"mainDialog::mainDialog(QWidget *parent) : QDialog(parent) { paintFrame = new QFrame; paintFrame->setFixedHeight(500); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(paintFrame); setLayout(mainLayout);}
C++ (Qt)#include <QApplication>#include "draw.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); mainDialog *dialog = new mainDialog; dialog->show(); return app.exec(); delete dialog;}
C++ (Qt) return app.exec();//вываливается из main delete dialog;//никогда сюда не попадем.
C++ (Qt)int main(int argc, char *argv[]) { QApplication app(argc, argv); mainDialog dialog; dialog.show(); return app.exec();}
C++ (Qt)int main(int argc, char *argv[]) { QApplication app(argc, argv); mainDialog *dialog = new mainDialog; dialog->show(); bool res = app.exec(); delete dialog; return res;}
C++ (Qt)int main(int argc, char *argv[]) { QApplication app(argc, argv); mainDialog *dialog = new mainDialog; dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->show(); return app.exec();}
#include <QtGui> #include "draw.h"mainDialog::mainDialog(QWidget *parent) : QDialog(parent) { paintFrame = new QFrame; paintFrame->setFixedHeight(500); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(paintFrame); setLayout(mainLayout);}void paintFrame::paintEvent(QPaintEvent *){ QPainter painter(this); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.drawText(rect(), Qt::AlignCenter, "Qt");}
C++ (Qt)#include "draw.h" mainDialog::mainDialog(QWidget *parent) : QDialog(parent) { paintFrame = new QFrame; paintFrame->installEventFilter(this);//устанавливаем фильтр событий paintFrame->setFixedHeight(500); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(paintFrame); setLayout(mainLayout);} bool mainDialog::eventFilter(QObject *o, QEvent *e){ if (o == painFrame && e->type() == QEvent::Paint) { //русуем здесь } return QDialog::eventFilter(o, e);}
C++ (Qt)#include <QApplication>#include "draw.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); mainDialog dialog; dialog.show(); return app.exec();}
C++ (Qt)#ifndef DRAW_H#define DRAW_H #include <QWidget> class mainDialog : public QWidget { Q_OBJECT public: mainDialog(QWidget *parent = 0); protected: virtual void paintEvent(QPaintEvent *);}; #endif
C++ (Qt)#include "draw.h" #include <QPainter> mainDialog::mainDialog(QWidget *parent) : QWidget(parent){} void mainDialog::paintEvent(QPaintEvent *){ QPainter painter(this); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.fillRect(rect(), Qt::red); painter.drawText(rect(), Qt::AlignCenter, tr("Hello Qt!"));}