Adding Custom QWidget to QMainWindow



  • Hello.

    I have QMainWindow based class GWindow with *.ui and also have custom QWidget based class Frame and I want to add Frame object to GWindow.

    GWindow ::GWindow (QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
        setWindowFlags(Qt::FramelessWindowHint);
      
        FrameObject.show();
        this->setCentralWidget(&FrameObject);
    }
    

    and Frame class . I added QPushButton for Frame UI in Qt Designer

    class Frame : public QWidget
    {
        Q_OBJECT
    
    public:
        Frame (QWidget *parent = 0);
        ~Frame ();
    
    private:
        Ui::Frame ui;
    };
    
    Frame ::Frame (QWidget *parent)
        : QWidget(parent)
    {
      ui.setupUi(this);
    }
    

  • Moderators

    what is FrameObject?
    Where and how is it defined?



  • @raven-worx said:

    is FrameObject?
    Where and how is it defined?

    into GWindow header

    class GWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        GWindow (QWidget *parent = 0);
        ~GWindow ();
    
    private:
        Ui::SandBoxClass ui;
        Frame FrameObject;    
    };
    

  • Moderators

    @Alatriste
    the issue has to be somewhere in the code you haven't posted yet. :)



  • @raven-worx

    Now WindowTitle instead of Frame and Sandbox intead of GWindow

    class WindowTitle : public QWidget
    {
        Q_OBJECT
    
    public:
        WindowTitle(QWidget *parent = 0);
        ~WindowTitle();
    
    private:
        Ui::WindowTitle ui;
    };
    
    
    WindowTitle::WindowTitle(QWidget *parent)
        : QWidget(parent)
    {
      ui.setupUi(this);
    }
    
    WindowTitle::~WindowTitle()
    {
    
    }
    
    #include <QtWidgets/QMainWindow>
    #include "WindowTitle.h"
    #include <QGridLayout>
    #include "ui_SandBox.h"
    
    class SandBox : public QMainWindow
    {
        Q_OBJECT
    
    public:
        SandBox(QWidget *parent = 0);
        ~SandBox();
    
    private:
        Ui::SandBoxClass ui;
        
    };
    
    #include "SandBox.h"
    
    SandBox::SandBox(QWidget *parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
      
        setWindowFlags(Qt::FramelessWindowHint);
    
        QWidget * widget = new QWidget;
        QGridLayout * layout = new QGridLayout;
        WindowTitle * w = new WindowTitle;
        
        w->show();
        layout->addWidget(w,0,0,1,1);
        widget->setLayout(layout);
        setCentralWidget(widget);
    }
    
    

  • Qt Champions 2016

    @Alatriste
    Hello,

    What about:

    SandBox::SandBox(QWidget * parent)
        :  QMainWindow(parent, Qt::FramelessWindowHint)
    {
        ui.setupUi(this);
    
        Ui::WindowTitle titleUi;  //< This could be put as a class member as well
        titleUi.setupUi(centralWidget());
    }
    

    You don't seem to be doing anything special with your WindowTitle class, so no need to derive QWidget only to set up a form.

    Kind regards.



  • @kshegunov

    But If I want use not fixed size widgets? How can I resize Ui::WindowTitle titleUi; custom widget?

    I want to set size dynamically, for example:

    mywidget->setGeometry(0,0,  parentWidget.widht(), 100);
    

  • Qt Champions 2016

    @kshegunov
    Hello,
    Ui::WindowTitle is your form class and not a widget. The widget I'm suggesting you initialize with it is the central widget of the main window, and the central widget will be resized automatically to fit the client area of the window.

    mywidget->setGeometry(0,0, parentWidget.widht(), 100);

    You shouldn't be wanting to do that. Just put the appropriate layout and setup your widgets in the designer. If any of them is fixed size put that in the appropriate properties of the object (the right side of the designer, after clicking on an object).

    Kind regards.


Log in to reply
 

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