Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Why I'm not secceed to change the posiyion of the button? helpp c++ qt

Why I'm not secceed to change the posiyion of the button? helpp c++ qt

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
layoutc++positionbutton
13 Posts 3 Posters 1.2k 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.
  • R RuWex
    2 Oct 2022, 07:52

    Hi!
    I wrote that code:

    QGridLayout *mainLayout = new QGridLayout;
         QPushButton *button = new QPushButton("START TESTING", this);
    
         mainLayout->addWidget(button, 5,6,5,2);
    
         button->show();
    

    but its ignore that line:

    mainLayout->addWidget(button, 5,6,5,2);
    

    Anyone know why???

    J Offline
    J Offline
    JonB
    wrote on 2 Oct 2022, 07:54 last edited by JonB 10 Feb 2022, 07:56
    #2

    @RuWex
    It does not "ignore that line". Your widget is added onto the QGridLayout, at the position and with the span you specified.

    Having said that, in the code you show the QGridLayout itself is not added onto any widget.

    1 Reply Last reply
    1
    • R Offline
      R Offline
      RuWex
      wrote on 2 Oct 2022, 07:57 last edited by RuWex 10 Feb 2022, 07:58
      #3

      @JonB so what can I do?

      J 1 Reply Last reply 2 Oct 2022, 07:59
      0
      • R RuWex
        2 Oct 2022, 07:57

        @JonB so what can I do?

        J Offline
        J Offline
        JonB
        wrote on 2 Oct 2022, 07:59 last edited by
        #4

        @RuWex What can you do in what way?

        R 1 Reply Last reply 2 Oct 2022, 08:00
        0
        • J JonB
          2 Oct 2022, 07:59

          @RuWex What can you do in what way?

          R Offline
          R Offline
          RuWex
          wrote on 2 Oct 2022, 08:00 last edited by
          #5

          @JonB I want to change the location, but only he currently in my program has a layout?

          J 1 Reply Last reply 2 Oct 2022, 08:07
          0
          • R RuWex
            2 Oct 2022, 08:00

            @JonB I want to change the location, but only he currently in my program has a layout?

            J Offline
            J Offline
            JonB
            wrote on 2 Oct 2022, 08:07 last edited by JonB 10 Feb 2022, 08:08
            #6

            @RuWex
            QLayouts have to be added onto QWidgets, so that when you add child widgets onto that you are controlling where they appear. Your this here must be a QWidget. Assuming that is the parent widget whose layout you want to set, you need to add

            this->setLayout(mainLayout);
            

            However, if your this is a QMainWindow --- I don't know because you don't say --- then that is not the right thing to do.

            R 1 Reply Last reply 2 Oct 2022, 08:16
            1
            • J JonB
              2 Oct 2022, 08:07

              @RuWex
              QLayouts have to be added onto QWidgets, so that when you add child widgets onto that you are controlling where they appear. Your this here must be a QWidget. Assuming that is the parent widget whose layout you want to set, you need to add

              this->setLayout(mainLayout);
              

              However, if your this is a QMainWindow --- I don't know because you don't say --- then that is not the right thing to do.

              R Offline
              R Offline
              RuWex
              wrote on 2 Oct 2022, 08:16 last edited by
              #7

              @JonB said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

              this->setLayout(mainLayout);

              yes, my this- *mainWindow:(
              so what can I do?

              A J 2 Replies Last reply 2 Oct 2022, 08:19
              0
              • R RuWex
                2 Oct 2022, 08:16

                @JonB said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                this->setLayout(mainLayout);

                yes, my this- *mainWindow:(
                so what can I do?

                A Offline
                A Offline
                albayenes
                wrote on 2 Oct 2022, 08:19 last edited by
                #8

                @RuWex

                @RuWex said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                @JonB said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                this->setLayout(mainLayout);

                yes, my this- *mainWindow:(
                so what can I do?

                Try this

                QWidget *widget = new QWidget();
                widget->setLayout(mainLayout);
                setCentralWidget(widget);

                1 Reply Last reply
                4
                • R RuWex
                  2 Oct 2022, 08:16

                  @JonB said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                  this->setLayout(mainLayout);

                  yes, my this- *mainWindow:(
                  so what can I do?

                  J Offline
                  J Offline
                  JonB
                  wrote on 2 Oct 2022, 08:29 last edited by JonB 10 Feb 2022, 08:30
                  #9

                  @RuWex
                  When you choose to use a QMainWindow it already has a very specific layout, shown in https://doc.qt.io/qt-6/qmainwindow.html#details. You should start by looking carefully at that. If you do not want the "furniture" added by QMainWindow --- like menu/toolbar/statusbar all surrounding a "central widget" --- then you probably should not be using it, e.g. a plain QWidget would probably suffice.

                  But let's say you do want a QMainWindow. Then in that picture you must put all your stuff in that Central Widget area. And you must start by giving that area a QWidget. And you can set that widget's layout. So for your case you probably want:

                  QWidget *centralWidget = new QWidget;
                  this->setCentralWidget(centralWidget);    // `this` is a `QMainWindow`
                  QGridLayout *mainLayout = new QGridLayout;
                  centralWidget->setLayout(mainLayout);
                  QPushButton *button = new QPushButton("START TESTING");
                  mainLayout->addWidget(button, 5,6,5,2);
                  
                  R 1 Reply Last reply 2 Oct 2022, 09:56
                  3
                  • J JonB
                    2 Oct 2022, 08:29

                    @RuWex
                    When you choose to use a QMainWindow it already has a very specific layout, shown in https://doc.qt.io/qt-6/qmainwindow.html#details. You should start by looking carefully at that. If you do not want the "furniture" added by QMainWindow --- like menu/toolbar/statusbar all surrounding a "central widget" --- then you probably should not be using it, e.g. a plain QWidget would probably suffice.

                    But let's say you do want a QMainWindow. Then in that picture you must put all your stuff in that Central Widget area. And you must start by giving that area a QWidget. And you can set that widget's layout. So for your case you probably want:

                    QWidget *centralWidget = new QWidget;
                    this->setCentralWidget(centralWidget);    // `this` is a `QMainWindow`
                    QGridLayout *mainLayout = new QGridLayout;
                    centralWidget->setLayout(mainLayout);
                    QPushButton *button = new QPushButton("START TESTING");
                    mainLayout->addWidget(button, 5,6,5,2);
                    
                    R Offline
                    R Offline
                    RuWex
                    wrote on 2 Oct 2022, 09:56 last edited by
                    #10

                    @JonB , @albayenes
                    I tried both of your ways
                    The problem is that it takes up the whole window for me and you can't see the rest of the window, and beyond that I want it to disappear after they click on it.
                    what can we do?

                    J 1 Reply Last reply 2 Oct 2022, 10:22
                    0
                    • R RuWex
                      2 Oct 2022, 09:56

                      @JonB , @albayenes
                      I tried both of your ways
                      The problem is that it takes up the whole window for me and you can't see the rest of the window, and beyond that I want it to disappear after they click on it.
                      what can we do?

                      J Offline
                      J Offline
                      JonB
                      wrote on 2 Oct 2022, 10:22 last edited by
                      #11

                      @RuWex said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                      I tried both of your ways

                      They are the same way, and they work, as per your question.

                      The problem is that it takes up the whole window for me and you can't see the rest of the window, and beyond that I want it to disappear after they click on it.

                      what can we do?

                      You need to define what you want, we don't know. You might want to show a screenshot with what you have and explain what you want.

                      R J 2 Replies Last reply 2 Oct 2022, 10:41
                      0
                      • J JonB
                        2 Oct 2022, 10:22

                        @RuWex said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                        I tried both of your ways

                        They are the same way, and they work, as per your question.

                        The problem is that it takes up the whole window for me and you can't see the rest of the window, and beyond that I want it to disappear after they click on it.

                        what can we do?

                        You need to define what you want, we don't know. You might want to show a screenshot with what you have and explain what you want.

                        R Offline
                        R Offline
                        RuWex
                        wrote on 2 Oct 2022, 10:41 last edited by
                        #12

                        @JonB
                        this is my window now:
                        825bcc04-f6aa-4751-8ca0-eced4f44bab1-image.png

                        and In the gray square there should have been a cube. that I opened
                        see what was before:
                        308f07b0-c14c-4982-8e0a-4c649c545f09-image.png

                        1 Reply Last reply
                        0
                        • J JonB
                          2 Oct 2022, 10:22

                          @RuWex said in Why I'm not secceed to change the posiyion of the button? helpp c++ qt:

                          I tried both of your ways

                          They are the same way, and they work, as per your question.

                          The problem is that it takes up the whole window for me and you can't see the rest of the window, and beyond that I want it to disappear after they click on it.

                          what can we do?

                          You need to define what you want, we don't know. You might want to show a screenshot with what you have and explain what you want.

                          J Offline
                          J Offline
                          JonB
                          wrote on 2 Oct 2022, 11:42 last edited by
                          #13

                          @JonB
                          You have (for unknown reason) placed a button at row #5 column #6 and spanning 5 rows and 2 columns. It's not going to make much sense, is perhaps not what you intend, and will move around when you place other widgets on the grid layout.

                          I know nothing about "In the gray square there should have been a cube. that I opened",

                          The second shot shows about what I expect when you didn't use any layout. If the 3 lines of text there were deliberate you would need to put them into the layout now if you expect to see them.

                          1 Reply Last reply
                          1

                          11/13

                          2 Oct 2022, 10:22

                          • Login

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