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. How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt
Forum Updated to NodeBB v4.3 + New Features

How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.5k Views 2 Watching
  • 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
    thippu
    wrote on last edited by
    #1

    Hi Guys,
    I Have a mainwindow.cpp file in that QMainWindow class is inherits the QMainWindow class and I Have plot.cpp file in this file I have Plot class which inherits the QwtPlot class.
    Now In the QMainWindow class constructor, I would like to add Plot object and QwtSlider object, by creating the QHboxLayout
    constructor code:

    MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),ui(new Ui::MainWindow),plot(0)
    {
    ui->setup(this);
    plot=new Plot(this); // This is one of the subclass of QwtPlot 
    QwtSlider *Slider=new QwtSlider(this);// QwtSlider class
     QHBoxLayout *mainLayout=new QHBoxLayout(this);
    mainLayout->addWidget(plot,60,Qt::AlignCenter);
    mainLayout->addWidget(slider);
    }
    
    

    Problem is application is not displaying the Plot widget in the center and Qwtslider is also attached to the Menubar.

    how to place them where ever we want on the mainwindow of the application?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Should you not add mainLayout to centralWidget ?
      centralWidget()->setLayout(mainLayout);

      T 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        QMainWindow already has a layout. The one that allows him to do its magic with dock widget, toolbars, etc.

        Therefore:

        @thippu said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:

        QHBoxLayout *mainLayout=new QHBoxLayout(this);

        won't work and should even show you a warning telling you that you are trying to set a layout on a widget that already has one.

        @mrjj's suggestions is the right one.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        T 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          QMainWindow already has a layout. The one that allows him to do its magic with dock widget, toolbars, etc.

          Therefore:

          @thippu said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:

          QHBoxLayout *mainLayout=new QHBoxLayout(this);

          won't work and should even show you a warning telling you that you are trying to set a layout on a widget that already has one.

          @mrjj's suggestions is the right one.

          T Offline
          T Offline
          thippu
          wrote on last edited by
          #4

          @SGaist said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:

          Hi,

          QMainWindow already has a layout. The one that allows him to do its magic with dock widget, toolbars, etc.

          Therefore:

          @thippu said in How to place two or more widgets where ever we want on the QMainWindow constructor or on the Application in Qt:

          QHBoxLayout *mainLayout=new QHBoxLayout(this);

          won't work and should even show you a warning telling you that you are trying to set a layout on a widget that already has one.

          @mrjj's suggestions is the right one.

          Yes, There was the warning.

          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            Should you not add mainLayout to centralWidget ?
            centralWidget()->setLayout(mainLayout);

            T Offline
            T Offline
            thippu
            wrote on last edited by
            #5

            @mrjj Thanks,
            I did combine all the widgets and kept them in one class and created an object of this class and set it as the centralWidget().
            I will definitely use the suggestion you have given.

            mrjjM 1 Reply Last reply
            0
            • T thippu

              @mrjj Thanks,
              I did combine all the widgets and kept them in one class and created an object of this class and set it as the centralWidget().
              I will definitely use the suggestion you have given.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @thippu
              Hi
              A composite / combined class is a good idea.
              Makes it easier to reuse/have more.

              T 1 Reply Last reply
              0
              • mrjjM mrjj

                @thippu
                Hi
                A composite / combined class is a good idea.
                Makes it easier to reuse/have more.

                T Offline
                T Offline
                thippu
                wrote on last edited by
                #7

                @mrjj Yes.

                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