Results 1 to 8 of 8

Thread: Problem with Parent QWidget and Buttons not working

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

    Default Problem with Parent QWidget and Buttons not working

    Ok, I have a class
    SomeObject,
    And underneath this
    I have two push buttons:
    QPushButton* a;
    QPushButton* b;

    Since I have like many SomeObjects, I cannot use
    QPushButton#a {
    to style. I am forced to set a parent.

    QWidget *AWidget;
    QWidget *BWidget;
    AWidget = new QWidget(this);
    BWidget = new QWidget(this);
    a = new QPushButton(AWidget);

    So now I can do:
    QWidget#AWidget > QPushButton#a {

    This works fine, but for some reason, every time i do this, one of the TWO buttons in this class, doesn't work, It's normal state works, but Hover and Pressed Doesn't, if i switch parents around create new parents etc, it makes no difference. If I switch A and B around as parents, it makes no difference. The only way to get the two buttons to work, is if I remove the AWidget and BWidget idea, but then I cannot STYLE these buttons... Remember SomeObject is a large array of SomeObject classes, and since QSS does NOT have "class" but only ID defined by setObjectName, I cannot do anything. I wish it was like CSS where you can just do like class='bla' on like 30 things, and then define how to style bla.

    Like as if there is some RULE in QT that you cannot have 2 Widgets with same widget parent.

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

    Default Re: Problem with Parent QWidget and Buttons not working

    Hmm, #-selector is for object name, so something like this should work:
    Qt Code:
    1. QPushButton* button = ..
    2. button->setObjectName("name");
    3. button->setStyleSheet("QPushButton#name {...}"); // ClassName#objectName
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    Default Re: Problem with Parent QWidget and Buttons not working

    If you want to style only one of the "SomeObject" objects then apply the stylesheet to that object only instead of the whole application. Another way is to subclass the button and then use the subclass name as a class selector.

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

    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 21:11.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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 

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

    VireX (11th May 2007)

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

    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.

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

    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

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.