Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to parent qpainter (code) to QTabWidget (Design Mode)

how to parent qpainter (code) to QTabWidget (Design Mode)

Scheduled Pinned Locked Moved Solved General and Desktop
parentqtablewidgetqmainwindowqpainterqt5.6
13 Posts 2 Posters 7.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P pauledd
    26 Aug 2016, 07:12

    My goal is to have the painter update every second so the image method might not be the way to do it.

    @mrjj said in how to parent qpainter (code) to QTabWidget (Design Mode):

    you need to subclass it and create a
    paintEvent for that.

    I need to read into subclassing...
    A subclass of what? Of the QTabWidget?

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 26 Aug 2016, 07:18 last edited by mrjj
    #4

    @pauledd

    • A subclass of what? Of the QTabWidget?

    If you want to draw on that directly.
    Still not clear how/where you want this drawing.

    Alternativ you can make a subclass of QWidget and just give it
    the paintEvent. And then place this into the TabWidget like any other widget and
    simply tell it to update() via the timer.

    class MyWidget : public QWidget
    {
    ...
    protected:
        void paintEvent(QPaintEvent *) { code };
    };
    

    You can easy use your own class in Designer with the promote feature
    http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html

    That let you design with a placeholder and when your code runs, its your widget.

    P 2 Replies Last reply 26 Aug 2016, 07:29
    1
    • M mrjj
      26 Aug 2016, 07:18

      @pauledd

      • A subclass of what? Of the QTabWidget?

      If you want to draw on that directly.
      Still not clear how/where you want this drawing.

      Alternativ you can make a subclass of QWidget and just give it
      the paintEvent. And then place this into the TabWidget like any other widget and
      simply tell it to update() via the timer.

      class MyWidget : public QWidget
      {
      ...
      protected:
          void paintEvent(QPaintEvent *) { code };
      };
      

      You can easy use your own class in Designer with the promote feature
      http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html

      That let you design with a placeholder and when your code runs, its your widget.

      P Offline
      P Offline
      pauledd
      wrote on 26 Aug 2016, 07:29 last edited by
      #5

      @mrjj said in how to parent qpainter (code) to QTabWidget (Design Mode):

      Still not clear how/where you want this drawing.

      It should go there, my old application is on the right, the new on the left:
      https://pauledd.files.wordpress.com/2016/08/parent2.jpg
      Its all static. It is fixed in position and the window size will not change.

      I will try your suggestions and figure out how it works, so far thank you.

      M 1 Reply Last reply 26 Aug 2016, 07:33
      0
      • P pauledd
        26 Aug 2016, 07:29

        @mrjj said in how to parent qpainter (code) to QTabWidget (Design Mode):

        Still not clear how/where you want this drawing.

        It should go there, my old application is on the right, the new on the left:
        https://pauledd.files.wordpress.com/2016/08/parent2.jpg
        Its all static. It is fixed in position and the window size will not change.

        I will try your suggestions and figure out how it works, so far thank you.

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 26 Aug 2016, 07:33 last edited by
        #6

        @pauledd
        Ok, thanks. very clear :)
        So thats actually a Tab Page Widget you want to draw on.
        I suggest to use the QWidget way as it easier than getting to draw directly on
        the Tabwidgets page child.

        1 Reply Last reply
        1
        • P Offline
          P Offline
          pauledd
          wrote on 26 Aug 2016, 08:21 last edited by
          #7

          Made it! (With costum widget and promote)
          https://pauledd.files.wordpress.com/2016/08/parent3.jpg

          The video that helped me:
          https://www.youtube.com/watch?v=Yt-YCxgEnyw

          Thanks @mrjj for directing to the solution!

          M 1 Reply Last reply 26 Aug 2016, 08:30
          2
          • P pauledd
            26 Aug 2016, 08:21

            Made it! (With costum widget and promote)
            https://pauledd.files.wordpress.com/2016/08/parent3.jpg

            The video that helped me:
            https://www.youtube.com/watch?v=Yt-YCxgEnyw

            Thanks @mrjj for directing to the solution!

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Aug 2016, 08:30 last edited by
            #8

            @pauledd
            Hi
            That was fast. :)
            Nice video. Good to know ! :)

            1 Reply Last reply
            0
            • M mrjj
              26 Aug 2016, 07:18

              @pauledd

              • A subclass of what? Of the QTabWidget?

              If you want to draw on that directly.
              Still not clear how/where you want this drawing.

              Alternativ you can make a subclass of QWidget and just give it
              the paintEvent. And then place this into the TabWidget like any other widget and
              simply tell it to update() via the timer.

              class MyWidget : public QWidget
              {
              ...
              protected:
                  void paintEvent(QPaintEvent *) { code };
              };
              

              You can easy use your own class in Designer with the promote feature
              http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html

              That let you design with a placeholder and when your code runs, its your widget.

              P Offline
              P Offline
              pauledd
              wrote on 31 Aug 2016, 13:00 last edited by
              #9

              @mrjj said in how to parent qpainter (code) to QTabWidget (Design Mode):

              Alternativ you can make a subclass of QWidget and just give it
              the paintEvent. And then place this into the TabWidget like any other widget and
              simply tell it to update() via the timer.

              Ok, I am back with more problems. I decided to not use the designer at all. I want to code it
              by myself to understand whats all behind that.

              A few lines of code. A mainwindow based on QWidget contains QGridLayout that contains QTabWidget that contains to tabs made of two QWidgets.

              A QPaintEvent that has "tab1" as parent. Why does it not paint that line?

              main.cpp

              #include <QApplication>
              #include <QTabWidget>
              #include <QWidget>
              #include "mainwindow.h"
              int main(int argc, char **argv){
              	
              	QApplication app(argc, argv);
              	
              	MainWindow mainWindow;
              	mainWindow.show();
              	return app.exec();
              }
              
              

              mainwindow.h

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              #include <QWidget>
              #include <QTabWidget>
              #include <QPainter>
              
              class MainWindow : public QWidget
              {
              	Q_OBJECT
              public:
              	explicit MainWindow(QWidget *parent = 0);
              protected:
              	void paintEvent(QPaintEvent *event);
              private:
              	QTabWidget *tabWidget;
              	QWidget *tab1;
              	QWidget *tab2;	
              };
              #endif // MAINWINDOW_H
              
              

              mainwindow.cpp

              #include "mainwindow.h"
              #include <QGridLayout>
              
              
              MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
              {
              	resize(800,480);
              	QGridLayout *gl = new QGridLayout(this);
              	tabWidget = new QTabWidget();
              	tab1 = new QWidget();
              	tab2 = new QWidget();
              	tabWidget->setStyleSheet("QTabWidget {font-size:20px;font-weight:bold}");
              	tabWidget->addTab(tab1,"Finder");
              	tabWidget->addTab(tab2,"Alignment");
              	gl->addWidget(tabWidget);
              }
              
              void MainWindow::paintEvent(QPaintEvent *event)
              {
              	QPainter painter(tab1);
              	painter.drawLine(0,0,800,480);
              
              }
              
              

              I also get this application output:

              Starting /home/paul/store/c++/build-pat_tab2-QT5_6_1-Debug/pat_tab2...
              QWidget::paintEngine: Should no longer be called
              QPainter::begin: Paint device returned engine == 0, type: 1
              
              1 Reply Last reply
              0
              • P Offline
                P Offline
                pauledd
                wrote on 31 Aug 2016, 14:06 last edited by
                #10

                I think I got it. I removed the tab1 widget and subclassed (I hope that is subclassing) it in a new class:

                mywidget.h

                #ifndef MYWIDGET_H
                #define MYWIDGET_H
                
                #include <QWidget>
                #include <QPainter>
                
                class MyWidget : public QWidget
                {
                Q_OBJECT
                	
                public:
                	MyWidget(QWidget *parent = 0);
                	~MyWidget();
                	
                protected:
                	void paintEvent(QPaintEvent *event);
                };
                
                
                #endif // MYWIDGET_H
                
                

                mywidget.cpp

                #include "mywidget.h"
                
                MyWidget::MyWidget(QWidget *parent)
                {
                	
                }
                
                MyWidget::~MyWidget()
                {
                	
                }
                
                void MyWidget::paintEvent(QPaintEvent *event)
                {
                	QPainter painter(this);
                	painter.drawLine(0,0,400,400);
                	
                }
                
                

                Then I replaced the declaration in mainwindow.h with " MyWidget *tab1;" and in mainwindow.cpp "tab1 = new MyWidget();".
                And that is how it looks:
                https://pauledd.files.wordpress.com/2016/08/tabwidget.jpg

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 31 Aug 2016, 21:26 last edited by kshegunov
                  #11

                  hi
                  just a note

                  void MainWindow::paintEvent(QPaintEvent *event)
                  {
                  	QPainter painter(tab1);
                  

                  seems a bit surprising.

                  void MainWindow::paintEvent(QPaintEvent *event)
                  {
                  	QPainter painter(this);
                  

                  would be as normally seen.

                  P 1 Reply Last reply 1 Sept 2016, 06:28
                  2
                  • M mrjj
                    31 Aug 2016, 21:26

                    hi
                    just a note

                    void MainWindow::paintEvent(QPaintEvent *event)
                    {
                    	QPainter painter(tab1);
                    

                    seems a bit surprising.

                    void MainWindow::paintEvent(QPaintEvent *event)
                    {
                    	QPainter painter(this);
                    

                    would be as normally seen.

                    P Offline
                    P Offline
                    pauledd
                    wrote on 1 Sept 2016, 06:28 last edited by pauledd 9 Jan 2016, 07:09
                    #12

                    @mrjj you are absolutely right. That was in my faulty first try. In the new tab1 QWidget subclass (mywidget.cpp) it is once more "QPainter painter(this);"
                    But thank you for hinting the wrong code :)

                    M 1 Reply Last reply 1 Sept 2016, 08:02
                    1
                    • P pauledd
                      1 Sept 2016, 06:28

                      @mrjj you are absolutely right. That was in my faulty first try. In the new tab1 QWidget subclass (mywidget.cpp) it is once more "QPainter painter(this);"
                      But thank you for hinting the wrong code :)

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 1 Sept 2016, 08:02 last edited by
                      #13

                      @pauledd
                      :)
                      class MyWidget : public QWidget << yes that is subclassing :)

                      Also, a widget can only paint on it self.
                      its not possible to paint on other widget from inside a
                      paintevent.
                      Thats why the first sample didnt work. You asked painter to paint on tab from inside mainwindows paintEvent :)
                      I guess you already found out but its just to make clear for other readers.

                      1 Reply Last reply
                      1

                      13/13

                      1 Sept 2016, 08:02

                      • Login

                      • Login or register to search.
                      13 out of 13
                      • First post
                        13/13
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved