I'm trying to add content to a QWidget
, but nothing ever shows. The window comes out blank, empty without any content I'm trying to include.
mainwindow.cpp
#include "mainwindow.h"
#include <QApplication>
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint)
{
mainWin = new QWidget();
// Create the button, make "this" the parent
m_button = new QPushButton("My Button", this);
// set size and location of the button
m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
hlayout = new QHBoxLayout;
hlayout -> addWidget(m_button);
mainWin -> setLayout(hlayout);
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QHBoxLayout>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private:
QPushButton *m_button;
QHBoxLayout *hlayout;
};
#endif
main.cpp
#include "mainwindow.h"
#include <QtPlugin>
#include <QApplication>
#include <QDesktopWidget>
Q_IMPORT_PLUGIN(BasicToolsPlugin)
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
QDesktopWidget dw;
int x=dw.width()*0.7;
int y=dw.height()*0.7;
window.setFixedSize(x, y);
window.show();
return app.exec();
}
What is it that I'm missing, or doing wrong?
Thank you all in advance.
Your code is not complete. I had to make some fixes (includes, declarations) to compile it.
Anyway, to start to see something, you have to either replace:
mainWin = new QWidget();
with
mainWin = new QWidget(this);
or replace:
mainWin -> setLayout(hlayout);
with
this -> setLayout(hlayout);
In the latter case it doesn't make sense calling:
m_button->setGeometry(QRect(QPoint(100, 100), QSize(200, 50)));
since position and size of m_button
is handled automatically by the layout.