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. Add QWidget containing other QWidgets onto QMainWindow

Add QWidget containing other QWidgets onto QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
qtwidgetswidgetsmainwindow
4 Posts 2 Posters 6.0k 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.
  • C Offline
    C Offline
    ChajusSaib
    wrote on last edited by ChajusSaib
    #1

    Hello there,

    I would like to add a class that inherits QWidget and contains QLineEdit, QPushButton... to a QMainWindow.

    When I do this however and call show() on the widgets there is a problem, they all have their own windows instead of being on the QMainWindow.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class SimpleStudyBox;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
    
    private:
        SimpleStudyBox *simple;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "simplestudybox.h"
    #include <QtWidgets>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        simple(new SimpleStudyBox())
    {
        simple->show();
        setCentralWidget(simple);
    }
    
    

    simplestudybox.h

    #ifndef SIMPLESTUDYBOX_H
    #define SIMPLESTUDYBOX_H
    
    #include <QWidget>
    
    class QLineEdit;
    class QPushButton;
    class QTextEdit;
    
    class SimpleStudyBox : public QWidget
    {
        Q_OBJECT
    public:
        explicit SimpleStudyBox(QWidget *parent = 0);
    
    private slots:
        void SHButtonClicked();
    
    private:
        void setupActions();
    
        QLineEdit *title;
        QPushButton *show_hide;
        QTextEdit *desc;
    };
    
    #endif // SIMPLESTUDYBOX_H
    
    

    simplestudybox.cpp

    #include "simplestudybox.h"
    #include <QtWidgets>
    
    SimpleStudyBox::SimpleStudyBox(QWidget *parent) : QWidget(parent), title(new QLineEdit()),
        show_hide(new QPushButton()), desc(new QTextEdit())
    {
        connect(show_hide, SIGNAL(clicked()), this, SLOT(SHButtonClicked()));
        centralLayout = new QGridLayout;
        centralLayout->addWidget(title);
        centralLayout->addWidget(show_hide);
        centralLayout->addWidget(desc);
        title->show();
        show_hide->show();
        desc->show();
    }
    
    void SimpleStudyBox::SHButtonClicked()
    {
        if (show_hide->text() == "Hide")
        {
            show_hide->setText("Show");
            desc->hide();
        }
        else
        {
            show_hide->setText("Hide");
            desc->show();
        }
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are not setting centralLayout as the layout of your SimpleStudyBox. And if you are using a layout to handle these and it's set properly, you don't need to call show on each of them, that will be done automatically.

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

      1 Reply Last reply
      2
      • C Offline
        C Offline
        ChajusSaib
        wrote on last edited by
        #3

        Thanks!

        #include "simplestudybox.h"
        #include <QtWidgets>
        
        SimpleStudyBox::SimpleStudyBox(QWidget *parent) : QWidget(parent), title(new QLineEdit()),
            show_hide(new QPushButton()), desc(new QTextEdit())
        {
            connect(show_hide, SIGNAL(clicked()), this, SLOT(SHButtonClicked()));
        
            QGridLayout *centralLayout = new QGridLayout;
            centralLayout->addWidget(title);
            centralLayout->addWidget(show_hide);
            centralLayout->addWidget(desc);
        
            setLayout(centralLayout);
        }
        
        void SimpleStudyBox::SHButtonClicked()
        {
            if (show_hide->text() == "Hide")
            {
                show_hide->setText("Show");
                desc->hide();
            }
            else
            {
                show_hide->setText("Hide");
                desc->show();
            }
        }
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You'r welcome !

          Since you have it working now, please mark the thread as solved using the "Topic Tool" button so other forum users may know a solution has been found :)

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

          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