Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting position of a widget
QtWS25 Last Chance

Setting position of a widget

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 17.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tanmay2227
    wrote on last edited by
    #1

    i want to set the position of a widget on top of another widget dynamically. how do i do it. what is the syntax can anyone help please. i want the new widget to be placed at exactly the same position where another is placed but on top of it

    Tanmay Priyadarshi

    1 Reply Last reply
    1
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,
      You can do this

      1. Get the position of the bottom widget
      2. Using these coordinates set the position for the new top widget

      For E.g:
      @
      QLabel *bottom = new QLabel(this);
      bottom->setGeometry(10,10,200,200);

      QLabel *top = new QLabel(this);
      top->setGeometry(bottom->x(),bottom->y(),200,200);

      // or

      top->move(bottom->x(),bottom->y());
      @

      157

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tanmay2227
        wrote on last edited by
        #3

        i tried that but the size of the widgets are not the same so it goes more down.
        i want the the bottom point of the widgets to be the same and not the top .
        so the bottom point of the widget should match with the bottom of the first one

        Tanmay Priyadarshi

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          Are the two widgets of the same size ?

          157

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tanmay2227
            wrote on last edited by
            #5

            no the first one is much smaller the width is same but the the height of the one to be positioned is much more

            Tanmay Priyadarshi

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              Then you need to do some calculations.
              Try this and see if this is what you want

              @
              QLabel *bottom = new QLabel(this);
              bottom->setGeometry(250,250,200,50);
              bottom->setStyleSheet("background-color: rgb(255,0,0)");

              QLabel *top = new QLabel(this);
              top->resize(200,200);
              top->move(bottom->x(),bottom->y()-(top->height()-bottom->height()));
              top->setStyleSheet("background-color: rgba(0,0,255,150)");
              @

              I have set the alpha component of the top widget so that we may see the effect.

              157

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tanmay2227
                wrote on last edited by
                #7

                it is appearing at that position but not on top of it i want it to appear on top of the previous widget but on doing this the previous widget just changes its position and appears over or under it .

                Tanmay Priyadarshi

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  This is because i have set the alpha component of the top widget to make it appear transparent so that we may see the bottom points of the two widgets coincide.
                  Change
                  @top->setStyleSheet("background-color: rgba(0,0,255,150)");@
                  to
                  @top->setStyleSheet("background-color: rgb(0,0,255)");@

                  and check if this is what you want

                  157

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tanmay2227
                    wrote on last edited by
                    #9

                    no i meant that the transparency is not happening it first component is moving its position they appear like this

                    list1


                    list2

                    while i want that they appear over one another
                    i want the second widget to be placed over every other widget like a menu is visible over the others

                    Tanmay Priyadarshi

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      Can you post you code or what you have tried? It would be hard to guess what would be the problem in your code.

                      157

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tanmay2227
                        wrote on last edited by
                        #11

                        list1 = new QListWidget;
                        list1->addItem("tamnmay");
                        list1->addItem("hghj");
                        list1->addItem("tamnmay");
                        list1->addItem("hghj");
                        list1->addItem("tamnmay");
                        list1->addItem("hghj");
                        list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

                        for(int i=0;i<list1->count()-1;i++)
                        {
                            list1->setRowHidden(i,true);
                        }
                        list1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Minimum);
                        list1->setMaximumWidth(1500);
                        
                        list1->installEventFilter(this);
                        
                        setMinimumSize(700, 500);
                        list1->setMaximumHeight(20);
                        setWindowState(Qt::WindowMaximized);
                        b1->setMaximumWidth(50);
                        b1->setFlat(true);
                        layout->addWidget(l1);
                        layout->addWidget(b1);
                        //layout->addWidget(list1);
                        widget->setLayout(layout);
                        widget->setStyleSheet("background-color: white; margin:0px");
                        QDockWidget *dock = new QDockWidget;
                        
                        
                        
                        dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
                          dock->setAllowedAreas(Qt::BottomDockWidgetArea );
                         customerList = new QListWidget();
                          customerList->addItems(QStringList()
                                  << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
                                  << "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
                                  << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
                                  << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
                                  << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
                                  << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
                          customerList->setWindowFlags(Qt::Popup);
                          customerList->setVisible(false);
                          QWidget *widget2 = new QWidget;
                          QVBoxLayout *lay = new QVBoxLayout;
                          //lay->addWidget(customerList);
                          lay->addWidget(list1);
                          widget2->setLayout(lay);
                        

                        QWidget* lTitleBar = dock->titleBarWidget();
                        QWidget* lEmptyWidget = new QWidget();
                        lEmptyWidget->setVisible(false);

                        statusBar()->addWidget(l1,0);
                        dock->setTitleBarWidget(lEmptyWidget);
                        delete lTitleBar;
                        dock->setWidget(widget2);
                        addDockWidget(Qt::BottomDockWidgetArea, dock);

                        }
                        bool MainWindow::eventFilter(QObject* object, QEvent* event )
                        {

                        if (object==list1)
                        {
                            if(event->type()==QEvent::Enter)
                            {
                                for(int i=1;i<list1->count();i++)
                                {
                        
                                    list1->setRowHidden(i,false);
                                }
                               //list1->setVisible(false);
                        
                                //customerList->setVisible(true);
                               // customerList->show();
                                customerList->setGeometry(list1->x(),list1->y(),300,500);
                        
                                menu=new QMenu(this);
                                menu->exec&#40;list1->pos(&#41;);
                                //list1->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                                //list1->setMaximumHeight(60);
                            }
                        }
                        

                        Tanmay Priyadarshi

                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #12

                          Please can you include it in Code tags. Its difficult to understand like this.

                          157

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            tanmay2227
                            wrote on last edited by
                            #13

                            sorry
                            @list1 = new QListWidget;
                            list1->addItem("tamnmay");
                            list1->addItem("hghj");
                            list1->addItem("tamnmay");
                            list1->addItem("hghj");
                            list1->addItem("tamnmay");
                            list1->addItem("hghj");
                            list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

                            for(int i=0;i<list1->count()-1;i++)
                            {
                                list1->setRowHidden(i,true);
                            }
                            list1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Minimum);
                            list1->setMaximumWidth(1500);
                            
                            list1->installEventFilter(this);
                            
                            setMinimumSize(700, 500);
                            list1->setMaximumHeight(20);
                            setWindowState(Qt::WindowMaximized);
                            b1->setMaximumWidth(50);
                            b1->setFlat(true);
                            layout->addWidget(l1);
                            layout->addWidget(b1);
                            //layout->addWidget(list1);
                            widget->setLayout(layout);
                            widget->setStyleSheet("background-color: white; margin:0px");
                            QDockWidget *dock = new QDockWidget;
                            
                            
                            
                            dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
                              dock->setAllowedAreas(Qt::BottomDockWidgetArea );
                             customerList = new QListWidget();
                              customerList->addItems(QStringList()
                                      << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
                                      << "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
                                      << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
                                      << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
                                      << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
                                      << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
                              customerList->setWindowFlags(Qt::Popup);
                              customerList->setVisible(false);
                              QWidget *widget2 = new QWidget;
                              QVBoxLayout *lay = new QVBoxLayout;
                              //lay->addWidget(customerList);
                              lay->addWidget(list1);
                              widget2->setLayout(lay);
                            

                            QWidget* lTitleBar = dock->titleBarWidget();
                            QWidget* lEmptyWidget = new QWidget();
                            lEmptyWidget->setVisible(false);

                            statusBar()->addWidget(l1,0);
                            dock->setTitleBarWidget(lEmptyWidget);
                            delete lTitleBar;
                            dock->setWidget(widget2);

                             addDockWidget(Qt::BottomDockWidgetArea, dock);
                            

                            }
                            bool MainWindow::eventFilter(QObject* object, QEvent* event )
                            {

                            if (object==list1)
                            {
                                if(event->type()==QEvent::Enter)
                                {
                                    for(int i=1;i<list1->count();i++)
                                    {
                            
                                        list1->setRowHidden(i,false);
                                    }
                                   //list1->setVisible(false);
                            
                                    //customerList->setVisible(true);
                                   customerList->show();
                                    customerList->setGeometry(list1->x(),list1->y(),300,500);
                                    //customerList->show();
                            
                                    menu=new QMenu(this);
                                    menu->exec&#40;list1->pos(&#41;);
                                    //list1->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
                                    //list1->setMaximumHeight(60);
                                }
                                if (event->type()==QEvent::ToolTip)
                                {
                            
                                }
                            }
                            if(object==customerList)
                            {
                                if(event->type()==QEvent::Leave)
                                {
                                    for(int i=0;i<list1->count()-1;i++)
                                    {
                                        list1->setRowHidden(i,true);
                                    }
                                    //list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                            
                                    //list1->setMaximumHeight(20);
                                    customerList->hide();
                                }
                            }
                            return false;
                            

                            }@

                            Tanmay Priyadarshi

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              Tabi
                              wrote on last edited by
                              #14

                              Is it possible to make next window as child of first it will show your next window always placed upon the parent and according to its geometry.

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                tanmay2227
                                wrote on last edited by
                                #15

                                but i dont want it to be a seperate window perhaps i can use the widget as a popup or tootip but i am not being able to implement it do you have any idea on how to do it or any other way to implement my requirement

                                Tanmay Priyadarshi

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved