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 subclass a control?

How to subclass a control?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 475 Views 2 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.
  • CesarC Offline
    CesarC Offline
    Cesar
    wrote on last edited by Cesar
    #1

    .h

    class MainWindow: public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindowClass ui;
    }
    
    
    class Mybutton : public QToolButton
    {
    public:
        Mybutton() { };
        QToolButton* Test(QWidget* parent = 0);
    
      protected:
    
        virtual void paintEvent(QPaintEvent* pQEvent) override;
    };
    
    
    

    .cpp

    QToolButton* Mybutton::Test(QWidget* parent)
    {
        QToolButton* newbtn = new QToolButton(parent);
        return newbtn;
    }
    
    
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        Mybutton my;
        QToolButton* newbtn = my.Test(this);
        newbtn->setGeometry(222, 222, 110, 70);
    }
    
    
    void Mybutton::paintEvent(QPaintEvent* pQEvent)
    {
        qDebug() << "test" << "\n";
    }
    

    The function Mybutton::paintEvent isn't getting called.

    1 Reply Last reply
    0
    • CesarC Offline
      CesarC Offline
      Cesar
      wrote on last edited by
      #2

      I got it working.

      Do subclassing controls increase my GUI CPU usage?
      is desirable to avoid subclass or is it 'ok'?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        @Cesar said in How to subclass a control?:

        got it working.

        I assume it looks a lot different to what you originally posted.

        Do subclassing controls increase my GUI CPU usage?

        No

        is desirable to avoid subclass or is it 'ok'?

        No. If you need to subclass then do so.

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          Hi @Cesar,

          The function Mybutton::paintEvent isn't getting called.

          Sounds like you figured it out, but in case others come across the same topic, the problem is that while Mybutton inherits QToolButton, Mybutton::Test() is returning a new QToolButton that does not.

          Instead of:

          class Mybutton : public QToolButton
          {
          public:
              Mybutton() { };
              QToolButton* Test(QWidget* parent = 0);
          ....
          

          do:

          class Mybutton : public QToolButton
          {
          public:
              Mybutton(QWidget * parent = 0) : QTootlButton(parent) { };
              // QToolButton* Test(QWidget* parent = 0); // You don't need this one.
          

          Then, in MainWindow::MainWindow(), instead of:

          QToolButton* newbtn = my.Test(this);
          

          do this:

          QToolButton* newbtn = new Mybutton(this);
          // or Mybutton * newbtn = new Mybutton(this);
          

          And yes, as @ChrisW67 said, it is ok to sub-class widgets - indeed, I'd say its recommended :)

          Cheers.

          CesarC 1 Reply Last reply
          2
          • Paul ColbyP Paul Colby

            Hi @Cesar,

            The function Mybutton::paintEvent isn't getting called.

            Sounds like you figured it out, but in case others come across the same topic, the problem is that while Mybutton inherits QToolButton, Mybutton::Test() is returning a new QToolButton that does not.

            Instead of:

            class Mybutton : public QToolButton
            {
            public:
                Mybutton() { };
                QToolButton* Test(QWidget* parent = 0);
            ....
            

            do:

            class Mybutton : public QToolButton
            {
            public:
                Mybutton(QWidget * parent = 0) : QTootlButton(parent) { };
                // QToolButton* Test(QWidget* parent = 0); // You don't need this one.
            

            Then, in MainWindow::MainWindow(), instead of:

            QToolButton* newbtn = my.Test(this);
            

            do this:

            QToolButton* newbtn = new Mybutton(this);
            // or Mybutton * newbtn = new Mybutton(this);
            

            And yes, as @ChrisW67 said, it is ok to sub-class widgets - indeed, I'd say its recommended :)

            Cheers.

            CesarC Offline
            CesarC Offline
            Cesar
            wrote on last edited by
            #5

            @Paul-Colby is possible to save the content drawn in the painter to when the function is called again avoid doing all process again?

            mrjjM 1 Reply Last reply
            0
            • CesarC Cesar

              @Paul-Colby is possible to save the content drawn in the painter to when the function is called again avoid doing all process again?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Cesar

              is possible to save the content drawn in the painter to when the function is called again avoid doing all process again?

              Well QPicture can do that.
              You can also render to a picture and only draw the picture.

              however, this is only used if the QPainter part is truly heavy.

              Not just for a perceived optimization.

              1 Reply Last reply
              1

              • Login

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