Results 1 to 2 of 2

Thread: [QT Creator] how to paint on widgets?

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default [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
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    i main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Drawing standard widgets using a custom paint engine
    By Waywocket in forum Qt Programming
    Replies: 26
    Last Post: 21st October 2010, 20:40
  2. Replies: 3
    Last Post: 6th October 2010, 23:00
  3. custom widgets in qt creator 4.6
    By tentakel in forum Newbie
    Replies: 3
    Last Post: 4th May 2010, 22:05
  4. add custom widgets to Qt Creator
    By yagabey in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2008, 17:26
  5. How to paint across/over multiple widgets?
    By richy in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2006, 10:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.