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 to QTabWidget?

How to parent QPainter to QTabWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 427 Views 1 Watching
  • 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.
  • C Offline
    C Offline
    centra
    wrote on last edited by
    #1

    Hi!

    I'm creating paint software which has tabs with Qt.
    I want to add the QPainter to each tab(Created with the QTabWidget).
    But the QPainter's function doesn't have the "QWidget *parent" property.
    How should I do?
    (I'm Japanese, so please forgive my poor English.)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #3

      Hi
      Use a custom widget for each tab so you can override paintEvent.

      #include <QPainter>
      #include <QWidget>
      
      class Canvas : public QWidget
      {
          Q_OBJECT
      public:
          explicit Canvas(QWidget *parent = nullptr) : QWidget(parent)
          {
      
          }
      protected:
          virtual void paintEvent(QPaintEvent *event) override
          {
              QPainter p(this);
              p.drawRect(0, 0, 100, 100);
          }
      };
      
      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        You can't attach a painter to something. You can create a QPainter in the QWidget::paintEvent() to draw on the widget.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        4
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #3

          Hi
          Use a custom widget for each tab so you can override paintEvent.

          #include <QPainter>
          #include <QWidget>
          
          class Canvas : public QWidget
          {
              Q_OBJECT
          public:
              explicit Canvas(QWidget *parent = nullptr) : QWidget(parent)
              {
          
              }
          protected:
              virtual void paintEvent(QPaintEvent *event) override
              {
                  QPainter p(this);
                  p.drawRect(0, 0, 100, 100);
              }
          };
          
          1 Reply Last reply
          3
          • C Offline
            C Offline
            centra
            wrote on last edited by
            #4

            I've got the thing I want to do. Thanks a lot!

            1 Reply Last reply
            0

            • Login

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