How to create a gradient like that from the style sheet



  • Hello,

    I have a tree view and I would like to color the background of some items with a gradient depending on what the user is doing. One possibility is that the user moves the mouse over a tree item. The only way to set the background to a gradient in this case is to define a style sheet like that and to set it as the tree views style sheet:

    QTreeView::item:hover {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgb(255, 255, 255),
    stop: 1 rgb (0, 255, 255);
    border: 1px solid rgb (0, 255, 255);
    }

    This works.

    There are situations where I would like to mark tree items the same way, even when the mouse is not placed over this tree item (there is a 3D view and some items of the 3D view correspondent to tree items - I would like to highlight the tree view items, when the user points on those items in the 3D view).

    This is also possible. I the data(...) method of the document tree model, I return a brush that I have created like this:

    QLinearGradient gradient(0, 0, 0, 1);
    gradient.setColorAt(0, QColor(255, 255, 255));
    gradient.setColorAt(255, QColor(0, 255, 255));
    QBrush brush(gradient);

    This also works. The only problem is, that the gradient defined with the brush is much different from the gradient defined with the style sheet, even though the numbers are different (actually in this example the complete background is white - but when I play with the numbers I realize that it is actually a gradient, but not the one I had expected).

    Could someone explain me the reason?

    I thought that this could be the case because the QLinearGradient interprets the x1, x2, y1, y2 value as pixels. Does the style sheet interpret these values as relative values? I have played around with the coordinate mode of the gradient, but this did not help.

    How can I make both gradients look the same? Or should I get rid of those style sheets and define everything with QBrush's?

    Thank you very much!
    Benjamin



  • I forgot to mention: I am using Qt 4.8.5.

    Am I the only one with this issue? Or is there no answer?

    My impression is that the style sheets are not really well-thought-out. I have tried now to set solid background colors for selections, because of my problem with the gradients. Even there I have problems: on my Linux a branch is displayed by default in my tree (this looks nice). When a tree item is selected, my color (as defined in the style sheet) is used for that selection, but the background color of the branch is a different one. I have tried to define that color with

    QTreeView::branch:selected { background: white; }

    but then no branch is displayed at all. :-(

    I don't like style sheets.


Log in to reply
 

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