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 Offline
    R Offline
    RuWex
    wrote on last edited by
    #1

    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???

    JonBJ 1 Reply Last reply
    0
    • R RuWex

      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???

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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 last edited by RuWex
        #3

        @JonB so what can I do?

        JonBJ 1 Reply Last reply
        0
        • R RuWex

          @JonB so what can I do?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @RuWex What can you do in what way?

          R 1 Reply Last reply
          0
          • JonBJ JonB

            @RuWex What can you do in what way?

            R Offline
            R Offline
            RuWex
            wrote on last edited by
            #5

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

            JonBJ 1 Reply Last reply
            0
            • R RuWex

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              1
              • JonBJ JonB

                @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 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 JonBJ 2 Replies Last reply
                0
                • R RuWex

                  @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 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

                    @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?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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
                    3
                    • JonBJ JonB

                      @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 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?

                      JonBJ 1 Reply Last reply
                      0
                      • R RuWex

                        @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?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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 JonBJ 2 Replies Last reply
                        0
                        • JonBJ JonB

                          @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 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
                          • JonBJ JonB

                            @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.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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

                            • Login

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