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. Swap widgets in place, without destroying them in layout.
QtWS25 Last Chance

Swap widgets in place, without destroying them in layout.

Scheduled Pinned Locked Moved General and Desktop
17 Posts 5 Posters 15.8k 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.
  • X Offline
    X Offline
    xcoder
    wrote on last edited by
    #1

    Hey,

    Is it possible to swap two or more widgets in place inside a layout, any layout?

    For example I have a vertical layout, with 2 widgets, I need to swap their places. If there wasn't a layout, I would just moved those widgets in each other coordinates. widget1->move(x,y);

    Is there a similar "move" command for layouts, where I can move widgets by rows and not by coordinates?

    Kind Regards
    Raivis

    Only a biker knows why a dog sticks his head out of a car window.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      There is an "example using QML.":http://labs.qt.nokia.com/2010/01/27/upcoming-qtcreator-support-for-qml-or-what-is-bauhaus/

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xcoder
        wrote on last edited by
        #3

        I don't want to use QML. That's why I posted here in Desktop section.
        Soory, I forgot to mention, that I want it to be old win32 style. :)

        Only a biker knows why a dog sticks his head out of a car window.

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Ok, And how would you like to trigger the "swap"? You won't have the nice movements like you do in QML. It will be instantaneously e.g. when you click a button the widgets are swapped. Is that what you want?

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • X Offline
            X Offline
            xcoder
            wrote on last edited by
            #5

            I know, I have two video feeds shown on those widgets. Like you said, I just want to swap them instantly. So is it possible?

            Only a biker knows why a dog sticks his head out of a car window.

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              I've not done it before, but I think it is possible.
              Just curious, why do you want that ? Can you explain some more about what your program is supposed to do? What kind of widgets do you want to use?

              I'll test it out tomorrow. Now it's time to get some sleep.

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xcoder
                wrote on last edited by
                #7

                I have two Video embedded plain QWidgets.
                And a vertical layout, which expands according to the size of Main window.
                Each widget is different size. To be more precise I need to swap those QWidget places, and one of them to reduce size, and the other to enlarge.

                I think it will be easier to show it with picture.

                !http://morf.lv/swap.jpg(SWAP)!

                The red border represents, the layout
                And those gray squares - QWidgets, which I want to swap, after click on a button, or the widget itself.

                I just want to remind once more, I don't want to delete them or hide them. I just want to simply move them.

                Thank you, sleep well, I'm off to bed as well.

                Only a biker knows why a dog sticks his head out of a car window.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  Haven't tackled the resizing issue, but with only two items, swapping the items may be as simple as calling setDirection() on your layout.

                  For example (pardon my rough code. I just threw this together):

                  widget.h
                  @
                  #ifndef WIDGET_H
                  #define WIDGET_H

                  #include <QWidget>
                  #include <QHBoxLayout>
                  #include <QLabel>

                  class Widget : public QWidget
                  {
                  Q_OBJECT

                  public:
                  Widget(QWidget *parent = 0);

                  protected slots:
                  void doSwap();

                  private:
                  QHBoxLayout *swapLayout;
                  QLabel *item1;
                  QLabel *item2;
                  };

                  #endif // WIDGET_H
                  @

                  widget.cpp:
                  @
                  #include "widget.h"

                  #include <QVBoxLayout>
                  #include <QPushButton>
                  #include <QLabel>

                  Widget::Widget(QWidget *parent) : QWidget(parent)
                  {
                  QVBoxLayout *vlayout = new QVBoxLayout(this);
                  vlayout->setSpacing(10);

                  swapLayout = new QHBoxLayout();
                  swapLayout->setSpacing(10);

                  item1 = new QLabel();
                  item1->setStyleSheet("background: red");
                  item1->setText("Item 1");
                  item1->setAlignment(Qt::AlignCenter);

                  swapLayout->addWidget(item1);

                  item2 = new QLabel();
                  item2->setStyleSheet("background: yellow");
                  item2->setText("Item 2");
                  item2->setAlignment(Qt::AlignCenter);
                  swapLayout->addWidget(item2);

                  vlayout->addLayout(swapLayout);

                  QPushButton *swapbutton = new QPushButton();
                  swapbutton->setText("Swap");

                  vlayout->addWidget(swapbutton);

                  connect(swapbutton, SIGNAL(clicked()), this, SLOT(doSwap()));
                  }

                  void Widget::doSwap()
                  {
                  if (swapLayout->direction() == QBoxLayout::LeftToRight) {
                  swapLayout->setDirection(QBoxLayout::RightToLeft);
                  } else {
                  swapLayout->setDirection(QBoxLayout::LeftToRight);
                  }
                  }
                  @

                  and a standard-issue main.cpp:
                  @
                  #include <QApplication>
                  #include "widget.h"

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  Widget w;
                  w.resize(300,300);
                  w.show();

                  return a.exec&#40;&#41;;
                  

                  }
                  @

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • EddyE Offline
                    EddyE Offline
                    Eddy
                    wrote on last edited by
                    #9

                    Nice work, mlong!

                    Seems there is Qt coding happening all around the clock. ;)

                    About the sizes: with mlong's solution, you could play with maximum sizes in the if then else block.

                    Is there anything else in the white zone on the right? If there is this could complicate things.

                    Qt Certified Specialist
                    www.edalsolutions.be

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      xcoder
                      wrote on last edited by
                      #10

                      Thanks, your solution is great, but later I'll need to use 3 or more widgets. That's why I wanted to swap their place in layout's row.

                      And this solution will work only for two widgets.

                      Best Regards
                      Raivis

                      Only a biker knows why a dog sticks his head out of a car window.

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dangelog
                        wrote on last edited by
                        #11

                        You could just remove them from the layout and re-add them in the correct positions and with the right sizing? From your mockup it seems to be implied that you're doing some kind of manual sizing on those widgets, is that correct?

                        Software Engineer
                        KDAB (UK) Ltd., a KDAB Group company

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #12

                          If you do it with remove re-add, using a grid layout makes it easier, as it allows empty cells. AFAIK box layouts remove emptzy cells.

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • X Offline
                            X Offline
                            xcoder
                            wrote on last edited by
                            #13

                            Exactly, I'm manually resizing them, but I don't want to remove those widgets and re-add, that's why I'm asking for another solution.

                            I have a video player embedded in those widgets from a LAN camera, I don't want to stop and start the stream.

                            If it's impossible I'll just start thinking of algorithm which aligns those widgets in main window, without using layouts, but pure coordinates.

                            But it would be nice, if it were possible to just swap them in layout.

                            Only a biker knows why a dog sticks his head out of a car window.

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dangelog
                              wrote on last edited by
                              #14

                              You don't delete nor hide the widgets. You just remove them from the layout and readd them.

                              Software Engineer
                              KDAB (UK) Ltd., a KDAB Group company

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mlong
                                wrote on last edited by
                                #15

                                [quote author="Eddy" date="1311913479"]Nice work, mlong!
                                Seems there is Qt coding happening all around the clock. ;)
                                [/quote]

                                Wasn't any big deal... you guys are the slackers that went to bed mid-afternoon! ;-)

                                Anyway...

                                Speaking of removing items from layouts, I was looking at that myself. I've never had to do that, actually, and could use some clarification on the proper way to do so.

                                Do you have to use takeAt() for that? If so, I see it returns a QLayoutItem*. That's where I get a little confused. Does the original widget have to be wrapped in a layout item to begin with, or does it just return this object from which you can extract the original widget? I haven't seen any good concrete examples of this in use, outside the descriptions in the docs.

                                Software Engineer
                                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  dangelog
                                  wrote on last edited by
                                  #16

                                  [quote author="mlong" date="1311946567"]

                                  Speaking of removing items from layouts, I was looking at that myself. I've never had to do that, actually, and could use some clarification on the proper way to do so.

                                  Do you have to use takeAt() for that? If so, I see it returns a QLayoutItem*. That's where I get a little confused. Does the original widget have to be wrapped in a layout item to begin with, or does it just return this object from which you can extract the original widget? I haven't seen any good concrete examples of this in use, outside the descriptions in the docs.
                                  [/quote]

                                  Yes. Use

                                  QLayout::count + itemAt to get the i-th item in a layout;

                                  QLayoutItem::widget to know 1) if it's a widget 2) which widget is that item wrapping

                                  QLayout::takeAt to remove the item from the layout

                                  an addItem overload (depending on the layout you're using) to readd it back in another position.

                                  You could also delete the QLayoutItem and create another one (remember that they don't own the widget they wrap).

                                  Software Engineer
                                  KDAB (UK) Ltd., a KDAB Group company

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mlong
                                    wrote on last edited by
                                    #17

                                    Ah, very good. Thanks a lot! That makes sense.

                                    Software Engineer
                                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                    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