November 25, 2010

linlin_0 linlin_0
Lab Rat
11 posts

[solved]background problem with QToolButton

 

hi all.
i have a QWidget object just as a box, and it contains some other children.
i want to use the box widget drawing background and children drawing icon only.
it works fine when i set the box’s background color only by stylesheet.
but when i set the box’s stylesheet just like “background-image: url(:/Images/xxx.png);”, it works bad,
the children also have the same iamge as the box, it looks like children repeating the image.
and i have tryed the function setAutoRaise(bool) just like “toolbutton->setAutoRaise(false);”, but it does not work.

any one can tell me what’s wrong with my code or how to solve it ?

Thanks in advance。

9 replies

November 25, 2010

Antonio Di Monaco Antonio Di Monaco
Lab Rat
117 posts

Hi,

you can enforce background role for your widget children. Just look at setBackgroundRole in QWidget documentation. I guess you can set it to QPalette::Button.

T.

November 25, 2010

linlin_0 linlin_0
Lab Rat
11 posts

i try it, and it also does not work.
there is my code.
i do’t know what’s wrong with it.

  1.     iTitleBox = new QWidget();
  2.     iTitleBox->setStyleSheet(QString(titleStyleSheet));
  3.     iTitleBox->setFixedHeight(60);
  4.  
  5.     QHBoxLayout* hlayout = new QHBoxLayout();
  6.  
  7.     hlayout->addSpacing(60);
  8.  
  9.     iTitle = new QLabel(tr("file"));
  10.     iTitle->setAlignment(Qt::AlignCenter);
  11.     iTitle->setAttribute(Qt::WA_TranslucentBackground, true);
  12.     hlayout->addWidget(iTitle);
  13.  
  14.     iBackButton = new QToolButton();
  15.     connect(iBackButton, SIGNAL(clicked()), this, SLOT(handleBack()));
  16.     iBackButton->setStyleSheet(QString(nullBackgroundStylesheet));
  17.     iBackButton->setBackgroundRole(QPalette::Button);
  18.     iBackButton->setIconSize(QSize(40, 40));
  19.     iBackButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
  20.     iBackButton->setFixedWidth(60);
  21.     iBackButton->setIcon(QIcon(QString(backIconPath)));
  22.     hlayout->addWidget(iBackButton);
  23.     iTitleBox->setLayout(hlayout);

November 25, 2010

Antonio Di Monaco Antonio Di Monaco
Lab Rat
117 posts

Hi,

I found a solution (I hope). When you set the parent widget stylesheet, you must specify that the stylesheet belongs only to the parent, and it’s not related to its children (notice the “Widget” word in the stylesheet, that’s the name of the class).

Example:

  1. class Widget : public QWidget
  2. {
  3.     Q_OBJECT
  4.  
  5.   QToolButton *tButton;
  6.   QLineEdit *lEdit;
  7.  
  8. public:
  9.     Widget(QWidget *parent = 0);
  10.     ~Widget();
  11. };

  1. Widget::Widget(QWidget *parent)
  2.     : QWidget(parent),
  3.   tButton(new QToolButton(this)),
  4.   lEdit(new QLineEdit(this))
  5. {
  6.   setStyleSheet("Widget { background-color: rgb(255,0,0); }");
  7.   tButton->move(100,100);
  8.  
  9.   lEdit->move(100,200);
  10. }
  11.  
  12. Widget::~Widget()
  13. {
  14.  
  15. }

T.

November 26, 2010

linlin_0 linlin_0
Lab Rat
11 posts
Antonio Di Monaco wrote:
Hi, I found a solution (I hope). When you set the parent widget stylesheet, you must specify that the stylesheet belongs only to the parent, and it’s not related to its children (notice the “Widget” word in the stylesheet, that’s the name of the class). Example:
  1.  class Widget : public QWidget { Q_OBJECT QToolButton *tButton; QLineEdit *lEdit; public: Widget(QWidget *parent = 0); ~Widget(); };
  1.  Widget::Widget(QWidget *parent) : QWidget(parent), tButton(new QToolButton(this)), lEdit(new QLineEdit(this)) { setStyleSheet("Widget { background-color: rgb(255,0,0); }"); tButton->move(100,100); lEdit->move(100,200); } Widget::~Widget() { }
T.

thanks for your reply.
but it still works bad.
i guess that your example will also work bad when you set the stylesheet with a background-image or border-image just like “QWidget{background-image: url(:/Images/xxx.png);}”.

Maybe I did not make it clear. I want the box (a QWidget object, created by “new QWidget”)show with a image for background, and children show with Transparent background (children are QToolButton object, and only have a icon.)。

November 26, 2010

Gerolf Gerolf
Robot Herder
3253 posts

If you want the ToolButton etc (the children) to be transparent, tell them to be…
Via a style sheet or via setting the properties (Widget attributes). Normally, that should work.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

November 26, 2010

linlin_0 linlin_0
Lab Rat
11 posts
Gerolf Reinwardt wrote:
If you want the ToolButton etc (the children) to be transparent, tell them to be… Via a style sheet or via setting the properties (Widget attributes). Normally, that should work.

thanks for your reply.
i have tryed “button->setAttribute(Qt::WA_TranslucentBackground, true);” and “button->setStyleSheet(“background-color:rgba(0,0,0,0);”);”, but it also do not work.

is there any other solution ?
thanks.

November 26, 2010

jonexi jonexi
Lab Rat
2 posts

I think following should do the trick

Set name for your widget by using ->setObjectName()
Add for your stylesheet:

  1. QWidget#yourWidgetName
  2. {
  3. background-image: url(image.jpg);
  4. }
  5.  
  6. {
  7. color: rgba(0, 255, 0, 100%);
  8. background-color: rgba(0, 0, 0, 0%);
  9. }

[edit: fixed @ tag / chetankjain]

November 29, 2010

linlin_0 linlin_0
Lab Rat
11 posts
jonexi wrote:
I think following should do the trick Set name for your widget by using ->setObjectName() Add for your stylesheet: QWidget#yourWidgetName { background-image: url(image.jpg); } QPushButton { color: rgba(0, 255, 0, 100%); background-color: rgba(0, 0, 0, 0%); }

great. This is the solution.

thanks a lot.

December 18, 2010

hisong1988 hisong1988
Lab Rat
72 posts

jonexi wrote:
I think following should do the trick

Set name for your widget by using ->setObjectName()
Add for your stylesheet:

[edit: fixed @ tag / chetankjain]

That is what I need,too. thanks jioexi and linlin_0 who post this thread.

 Signature 

I am still searching for….

 
  ‹‹ [Solved] Invoking external applications to view, edit and print documents      Moc Error: Too many input files specified. Integration Problem? ››

You must log in to post a reply. Not a member yet? Register here!