PDA

View Full Version : Problem with Parent QWidget and Buttons not working



VireX
11th May 2007, 08:13
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.

jpn
11th May 2007, 08:20
Hmm, #-selector (http://doc.trolltech.com/4.2/stylesheet.html#selector-types) is for object name, so something like this should work:


QPushButton* button = ..
button->setObjectName("name");
button->setStyleSheet("QPushButton#name {...}"); // ClassName#objectName

wysota
11th May 2007, 08:59
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.

VireX
11th May 2007, 20:05
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.

wysota
11th May 2007, 21:20
This works fine (maybe you forgot about Q_OBJECT?)


#include <QApplication>
#include <QPushButton>
#include <QLayout>

class MyPushButton : public QPushButton {
Q_OBJECT
public:
MyPushButton(QWidget *parent=0) : QPushButton(parent){}
};

#include "main.moc"

int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget w;
QVBoxLayout *l = new QVBoxLayout(&w);
l->addWidget(new MyPushButton);
l->addWidget(new QPushButton);
app.setStyleSheet(".MyPushButton{ border: 2px solid black; }");
w.show();
return app.exec();
}

VireX
11th May 2007, 21:47
Thanks thats what was missing, I thought Q_OBJECT is only if i use SLOT/SIGNALs.

jpn
11th May 2007, 22:09
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 (http://doc.trolltech.com/4.2/metaobjects.html) and QMetaObject).

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.

wysota
11th May 2007, 22:24
Not only those, but also other meta-object system stuff (see Meta-Object System (http://doc.trolltech.com/4.2/metaobjects.html) 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.