PDA

View Full Version : [QT Creator] how to paint on widgets?



Tomasz
13th July 2010, 11:48
Hello!

I've started to learn QT4, and I've got a problem. I've created simple application with QTCreator - one window with two tabs on it (QTabWidget with tab and tab_2), Now I've got 3 files:

mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}


i main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


Now I want to draw something with QPainter on tab_2. I've read a lot of tutorails but I didn't found it. What should I write and where? I can only paint on MainWindow with MainWindow::PaintEvent. If it's not a problem can someone write me simple code which will work?

thanks in advance
best regards
Tomasz

wysota
13th July 2010, 14:27
There are two ways of doing what you want. A shorter (yet not as powerful) one is to install a so called "event filter" on the tab and intercept its paint event elsewhere (i.e. in your main window). The other solution is to make your tab_2 a custom widget and reimplement its paint Event. Try to use the second version, it's much more appropriate in OOP in general.