Results 1 to 8 of 8

Thread: Problem with Parent QWidget and Buttons not working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Problem with Parent QWidget and Buttons not working

    Yeah see, what I do is I have a QSS file outside the program, and i read that at beginning of the program, its more OO than having to do setStytleSheet each time.

    I wish there was an easier way like setStyleName, so that I can just write that in the CSS :P. I guess subclassing isn't too big a problem though :P. Thanks.

    jpn, I know about setobject.

    PErhaps I could use setProperty. Also when I subclasses QPushButton, I could not define the QSS for it in my file. I tried:
    .MyButton {
    QPushButton[class="MyButton"] {
    QPushButton.MyButton {
    MyButton {

    It seems sub classing cannot work.
    Last edited by VireX; 11th May 2007 at 20:11.

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

    Default Re: Problem with Parent QWidget and Buttons not working

    This works fine (maybe you forgot about Q_OBJECT?)

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLayout>
    4.  
    5. class MyPushButton : public QPushButton {
    6. Q_OBJECT
    7. public:
    8. MyPushButton(QWidget *parent=0) : QPushButton(parent){}
    9. };
    10.  
    11. #include "main.moc"
    12.  
    13. int main(int argc, char **argv){
    14. QApplication app(argc, argv);
    15. QVBoxLayout *l = new QVBoxLayout(&w);
    16. l->addWidget(new MyPushButton);
    17. l->addWidget(new QPushButton);
    18. app.setStyleSheet(".MyPushButton{ border: 2px solid black; }");
    19. w.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to wysota for this useful post:

    VireX (11th May 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    209
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: Problem with Parent QWidget and Buttons not working

    Thanks thats what was missing, I thought Q_OBJECT is only if i use SLOT/SIGNALs.

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Problem with Parent QWidget and Buttons not working

    Quote Originally Posted by VireX View Post
    I thought Q_OBJECT is only if i use SLOT/SIGNALs.
    Not only those, but also other meta-object system stuff (see Meta-Object System and QMetaObject).
    Quote Originally Posted by http://doc.trolltech.com/4.2/qobject.html#Q_OBJECT
    The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
    J-P Nurmi

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

    Default Re: Problem with Parent QWidget and Buttons not working

    Quote Originally Posted by jpn View Post
    Not only those, but also other meta-object system stuff (see Meta-Object System and QMetaObject).
    Or the other way round as signals and slots use the meta-object system and not vice versa. So Q_OBJECT enables creation of a meta-object for the class which implies an ability to declare signals and slots.

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
  •  
Qt is a trademark of The Qt Company.