[QT Creator] how to paint on widgets?
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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
i main.cpp
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *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
Re: [QT Creator] how to paint on widgets?
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.