arqam arqam - 1 year ago 50
C++ Question

Creating QWidget in a class

I am trying to create a window in my class.

I went through the documentation :

QApplication a(argc, argv);
QWidget window;;
return a.exec();

This does show the window when the code is in main.cpp .

But I want to create the window in other class.
When I use the line :

QWidget window;;

It doesn't give me the window, and the program also doesn't quit.

So how can we create a widget in QT in our own class?

Answer Source

This a sample QWidget that initialized and customized:

#include <QWidget>

void FileManager::initializeMyWidget()
    QWidget *myWidget= new QWidget();
    myWidget->setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    myWidget->setWindowFlags(windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
    QIcon iconMyWidget(":/Images/Images/image.png");
    QHBoxLayout *mainLayout = new QHBoxLayout;

Now call function and show the widget:


You can download this sample from here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download