[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。



  • 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.



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

    @

    iTitleBox = new QWidget();
    iTitleBox->setStyleSheet(QString(titleStyleSheet));
    iTitleBox->setFixedHeight(60);
    
    QHBoxLayout* hlayout = new QHBoxLayout();
    
    hlayout->addSpacing(60);
    
    iTitle = new QLabel(tr("file"));
    iTitle->setAlignment(Qt::AlignCenter);
    iTitle->setAttribute(Qt::WA_TranslucentBackground, true);
    hlayout->addWidget(iTitle);
    
    iBackButton = new QToolButton();
    connect(iBackButton, SIGNAL(clicked()), this, SLOT(handleBack()));
    iBackButton->setStyleSheet(QString(nullBackgroundStylesheet));
    iBackButton->setBackgroundRole(QPalette::Button);
    iBackButton->setIconSize(QSize(40, 40));
    iBackButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
    iBackButton->setFixedWidth(60);
    iBackButton->setIcon(QIcon(QString(backIconPath)));
    hlayout->addWidget(iBackButton);
    iTitleBox->setLayout(hlayout);
    

    @



  • 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:

    @
    class Widget : public QWidget
    {
    Q_OBJECT

    QToolButton *tButton;
    QLineEdit *lEdit;

    public:
    Widget(QWidget *parent = 0);
    ~Widget();
    };
    @

    @
    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.



  • [quote author="Antonio Di Monaco" date="1290679063"]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: @ class Widget : public QWidget { Q_OBJECT QToolButton *tButton; QLineEdit *lEdit; public: Widget(QWidget *parent = 0); ~Widget(); }; @ @ 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.[/quote]

    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.)。



  • 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.



  • [quote author="Gerolf Reinwardt" date="1290759514"]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.[/quote]

    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.



  • 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%);
    }
    @

    [edit: fixed @ tag / chetankjain]



  • [quote author="jonexi" date="1290768612"]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%); } [/quote]

    great. This is the solution.

    thanks a lot.



  • [quote author="jonexi" date="1290768612"]I think following should do the trick

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

    [edit: fixed @ tag / chetankjain][/quote]

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.