Results 1 to 6 of 6

Thread: inherited QPushButton is not being displayed in a layout

  1. #1
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default inherited QPushButton is not being displayed in a layout

    Hello,
    I want to create a new QPushButton, so I inherited it like this:
    Qt Code:
    1. class MyButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. MyButton(const QString &text, QWidget *parent = 0);
    6.  
    7. };
    To copy to clipboard, switch view to plain text mode 

    the code of the constructor looks like this:
    Qt Code:
    1. MyButton::MyButton(const QString &text, QWidget *parent)
    2. {
    3. btn = new QPushButton(text);
    4. btn->setFlat(true);
    5. //btn->show();
    6. btn->setMaximumHeight(30);
    7. }
    To copy to clipboard, switch view to plain text mode 

    When I now add a new MyButton to my MainWindow widget it does not show up, but when I remove the two slashes before btn->show(), the new button is drawn in a separate window. Why does the button only appear in a separate window and not in my main widget?

    Here is the code snipped from the main widget:
    Qt Code:
    1. Btn1 = new MyButton("Hello World");
    2. layout->addWidget(Btn1);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: inherited QPushButton is not being displayed in a layout

    You are confused about how inheritance works. You shouldn't be creating a new QpushButton.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: inherited QPushButton is not being displayed in a layout

    MyButton is-a QPushButton: this is not a contains-a relationship. You are setting up an aggregated push button when what you want to be doing is constructing the QPushButton underlying your MyButton object.
    Qt Code:
    1. class MyButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. MyButton(const QString &text, QWidget *parent = 0);
    6. };
    7.  
    8. MyButton::MyButton(const QString &text, QWidget *parent): QPushButton(text, parent)
    9. {
    10. setFlat(true);
    11. setMaximumHeight(30);
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2011
    Posts
    27
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: inherited QPushButton is not being displayed in a layout

    @Chris, I tried your code, but the button does not show up, it seems to be invisible. I created a connection that if the user clicks the button a messagbox appears. And when I click in the empty area the messagebox pops up. But why is my button invisible? I also created a paintEvent which should draw a rectangle around the button

    Qt Code:
    1. void MyButton::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. painter.setPen(Qt::black);
    6.  
    7. painter.begin(this);
    8.  
    9. painter.drawRect(this->pos().x(), this->pos().y(), this->pos().x() + this->width(), this->pos().y() + this->height());
    10.  
    11. painter.end();
    12. }
    To copy to clipboard, switch view to plain text mode 

    but there is no rectangular. In the Application Output QtCreator says
    QPainter::begin: Painter already active
    when the curser is over the button.

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: inherited QPushButton is not being displayed in a layout

    Call base class implementation in paintEvent as well:
    Qt Code:
    1. void MyButton::paintEvent(QPaintEvent *event)
    2. {
    3. QPushButton::paintEvent(event);
    4. //... your code here
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: inherited QPushButton is not being displayed in a layout

    This is the correct way to do QPushButton subclassing

    Qt Code:
    1. class MyButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. MyButton(const QString &text, QWidget *parent = 0);
    6. };
    7.  
    8. MyButton::MyButton(const QString &text, QWidget *parent) : QPushButton(text, parent)
    9. {
    10. setFlat(true);
    11. setMaximumHeight(30);
    12. }
    13.  
    14. //main widget code
    15. Btn1 = new MyButton("Hello World");
    16. layout->addWidget(Btn1);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2010, 10:30
  2. Inherited Style Sheet
    By johnmauer in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2010, 22:07
  3. How QMessageBox can be inherited.
    By rajveer in forum Qt Programming
    Replies: 40
    Last Post: 10th September 2008, 15:27
  4. Replies: 3
    Last Post: 26th September 2006, 13:16
  5. qpushbutton and layout
    By mickey in forum Newbie
    Replies: 1
    Last Post: 11th March 2006, 15:58

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.