create QT project without using form or qml



  • I have some experience in creating x11 applications using xlib. I am new to QT and I don't want to use any of the xlib calls.
    I used an empty qt project and added necessary class files in it. I used separate main.cpp and class for mainwindow. Now i would like to create only Qlabel in a separate class and assign mainwindow as it's parent.
    Here is my code:
    /main.cpp/

    #include <QtGui/QApplication>
    #include <mainwindow.h>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        mainWindow mainWin;
        mainWin.showFullScreen();
        a.applicationName();
    
        return a.exec();
    }
    

    /mainwindow.h/

    #include <QMainWindow>
    
    class mainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit mainWindow(QWidget *parent = 0);
        
    signals:
        
    public slots:
        
    };
    

    /mainwindow.cpp/

    #include "mainwindow.h"
    
    mainWindow::mainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
    }
    

    /baseTextWin.h/

    #include <QLabel>
    #include <QString>
    class baseTextWin
    {
    public:
        QLabel* textWin;
    
        baseTextWin(,int x, int y, int width, int height); //would like to set mainwindow as parent
        void showTextWin();
        void hideTextWin();
    };
    

    /baseTextWin.cpp/

    #include "basetextwin.h"
    
    baseTextWin::baseTextWin(int x, int y, int width, int height)
    {
        textWin = new QLabel;
        textWin->setGeometry(x, y, width, height);
    
        hideTextWin();
    }
    void baseTextWin::showTextWin() {
        textWin->show();
    }
    
    void baseTextWin::hideTextWin() {
        textWin->hide();
    }
    

    now when I execute this, I get as separate windows. I want everything in one main window.


  • Moderators

    Hi @PopQt, you said you "would like to set mainwindow as parent". To do that, pass the parent widget's pointer into the child widget's constructor.

    Example:

    mainWindow::mainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        // Create a QLabel which shows the word "Hello",
        // and is nested inside this main window
        auto label = new QLabel("Hello", this);
        // ...
    }
    


  • Thank you very much @JKSH . It worked.


Log in to reply
 

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