Add QWidget containing other QWidgets onto QMainWindow



  • 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();
        }
    }
    
    

  • Lifetime Qt Champion

    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.



  • 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();
        }
    }
    
    

  • Lifetime Qt Champion

    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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.