Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Objects switching on my HMI



  • This post is deleted!


  • Still have the same problem :) Up !


  • Moderators

    @Chanchan
    I'm not entirely sure what you want to do.

    Something like this ?

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWidget parent;
        parent.show();
    
        QWidget *widget1 = new QWidget;
        widget1->setStyleSheet("background-color: red;");
        widget1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QWidget *widget2 = new QWidget;
        widget2->setStyleSheet("background-color: blue;");
        widget2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QPushButton *btn = new QPushButton("Switch");
    
        QGridLayout *layout = new QGridLayout(&parent);
        layout->addWidget(widget1,0,0,3,1);
        layout->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding),0,1,1,1);
        layout->addWidget(widget2, 1,1,1,1);
        layout->addItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding),2,1,1,1);
        layout->addWidget(btn, 1,2,1,1);
    
        auto switchAndResize = [widget1, widget2, layout]()->void{
            int i1 = layout->indexOf(widget1);
            int i2 = layout->indexOf(widget2);
            layout->removeWidget(widget1);
            layout->removeWidget(widget2);
            if( i1 < i2){
                layout->addWidget(widget2,0,0,3,1);
                layout->addWidget(widget1,1,1,1,1);
            } else {
                layout->addWidget(widget1,0,0,3,1);
                layout->addWidget(widget2,1,1,1,1);
            }
        };
    
        QObject::connect(btn, &QPushButton::clicked, [=]()->void{switchAndResize();});
    
        return  a.exec();
    }
    
    


  • YES ! This is exactly what I was searching for !!
    I didn't know "auto switchandresize"

    I will try to implement your exemple with my project :3 thank you very much ! :)



  • @J.Hilk Hey ! So I worked on some implementation based on your code (first without the QSpacer Item because I'm not very good with the use of it). It worked well but I have some places problems.
    I know that I need your QSpacer Item to avoid these problem that's why I tried to understand the use of it. But ... I have some difficulties about it so....
    On the documentation they define it as : QSpacerItem(int w, int h, QSizePolicy::Policy hPolicy = QSizePolicy::Minimum, QSizePolicy::Policy vPolicy = QSizePolicy::Minimum)

    so : w is to define the size on the horizontal, h on the vertical (I dont understand why it is necessary to define this size)
    then h Policy and v policy are define to explain in which direction the spacer will "forced" position, is that right ?

    Thanks agaon for your help and sorry I try to be as clear as possible but if it's not I will send some drawpicture I think ^^)



  • This post is deleted!

  • Moderators

    @Chanchan said in Objects switching on my HMI:

    so : w is to define the size on the horizontal, h on the vertical (I dont understand why it is necessary to define this size)

    IIRC it's the size hint/ minimal size in case you Spacer has the size policy flags QSizePolicy::Prefered or QSizePolicy::Minimum

    then h Policy and v policy are define to explain in which direction the spacer will "forced" position, is that right ?

    More or less, it defines its behavior in vertical and horizontal orientation when placed in a layout when taking the size policy both the other layout objects into consideration.

    If it can Help you to visualize the situation ahah x)

    if your original layout works, (before button for switching is pressed) you can read the sizeHint() sizePolicy() and minimalSize() maximalSize() values of your 2 different widgets and swap them as well.

    on the other hand, here's the modified example to your layout.

    #include <QApplication>
    #include <QtWidgets>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWidget parent;
        parent.show();
    
        QLabel *video = new QLabel("video");
        video->setStyleSheet("background-color:red;");
        video->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QLabel *map = new QLabel("Map");
        map->setStyleSheet("background-color: blue;");
        map->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        QPushButton *btn = new QPushButton("Switch");
    
        QGridLayout *layout = new QGridLayout(&parent);
        layout->addWidget(video,0,0,2,1);
        layout->addWidget(btn, 1,1,1,1);
        layout->addWidget(map, 2,1,1,1);
    
        layout->setRowStretch(0,8);
        layout->setRowStretch(1,1);
        layout->setRowStretch(2,4);
        layout->setColumnStretch(0,17);
        layout->setColumnStretch(1,4);
    
        auto switchAndResize = [video, map, layout]()->void{
            int i1 = layout->indexOf(video);
            int i2 = layout->indexOf(map);
            layout->removeWidget(video);
            layout->removeWidget(map);
            if( i1 < i2){
                layout->addWidget(map,0,0,2,1);
                layout->addWidget(video,2,1,1,1);
            } else {
                layout->addWidget(video,0,0,2,1);
                layout->addWidget(map,2,1,1,1);
            }
        };
    
        QObject::connect(btn, &QPushButton::clicked, [=]()->void{switchAndResize();});
    
        return  a.exec();
    }
    

    0_1554742298947_50f45498-85b9-4126-b092-1b4396e64535-image.png



  • @J.Hilk Wow so much help ! Thank you very much, thanks to you I have exactly what I was searching for :) Thanks a lot !
    I put solved on the subject !


Log in to reply