Combine multiple Objects to one



  • Hey,

    is it possible to combine multiple objects to one? I have 5 QFrame line objects. Now I want them to change color on hovering. As far as I know, the best way to do this is to edit the style sheet of an object with

    #line::hover {
    border: 5px solid black;
    }
    

    Now I want to change the color of all my lines if I hover one of them. So can I combine the line objects to one object and give them one style sheet so that all of them change their color if I hover one?

    Thanks!


  • Moderators

    @Beatsteak said in Combine multiple Objects to one:

    Now I want to change the color of all my lines if I hover one of them. So can I combine the line objects to one object and give them one style sheet so that all of them change their color if I hover one?

    This should give you an idea:

    #line[MyHoveredProperty="true"] {
    border: 5px solid black;
    }
    
    void RepolishWidget( QWidget* widget )
    {
            widget->style()->unpolish(widget );
            widget->style()->polish(widget );
            QEvent event(QEvent::StyleChange);
            QApplication::sendEvent(widget, &event);
            widget->update();
            widget->updateGeometry();
    }
    ...
    bool hovered;
    foreach( QFrame* frame, frames )
    {
         frame->setProperty("MyHoveredProperty", hovered);
         frame->style()->polish(frame); // in case the content margins (border-width, padding, margin) changes with your CSS you need to call RepolishWidget() instead, otherwise this is enough
    }
    


  • Thanks for your answer, this sounds like what I was looking for. But where do I have to put this code? The putter part seems to belong into the style sheet? And where do I put the rest of your code?


  • Moderators

    @Beatsteak said in Combine multiple Objects to one:

    And where do I put the rest of your code?

    depends on how your code looks like.

    For example you can install an event filter on your frame widgets and listen for a QEvent::Enter / QEvent::Leave event types and set the property of all your frames.

    Or you subclass QFrame and override the enterEvent() / leaveEvent() handlers there and emit a signal. Then in a connected slot set the properties of all frames.

    Putting your frame widgets into a list (like in my example) lets you quickly traverse them with a minimum amount of code.


Log in to reply
 

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