Problem in displaying window?
-
This is my code please check it what is the problem
@#include <QtGui>
#include <QObject>
#include "mainwindow.h"
#include "window1.h"class MyWindow: public QWidget
{Q_OBJECT
public:
MyWindow(QWidget *parent = 0):QWidget(parent)
{
qDebug ()<<"window constructor";
float button_width = 47;
float button_height = 47;
int button_states = 1;QMainWindow new_window; QPixmap pixmap(":/endcall.png"); Button *button1 = new Button(button_width, button_height, button_states, pixmap, this); button1->setGeometry(60, 70, 80, 90); QObject::connect(button1, SIGNAL(pressed()), &new_window, SLOT(show())); //QObject::connect(button1, SIGNAL(pressed()), button1, SLOT(press())); pixmap.load(":/und_speaker.png"); button_width = 17; button_height = 14; Button *button2 = new Button(button_width, button_height, 1, pixmap, this); button2->setGeometry(100,110,120,130); pixmap.load(":/audio.png"); button_width = 17; button_height = 16; Button *button3 = new Button(button_width, button_height, 2, pixmap, this); button3->setGeometry(130,140,150,160); } ~ MyWindow(){} void mousePressEvent(QMouseEvent *k) { qDebug ()<<"window mouse press event"; QWidget::mousePressEvent(k); } void mouseReleaseEvent(QMouseEvent *h) { qDebug ()<<"window mouse release event"; QWidget::mouseReleaseEvent(h); }
};@
in the above code "new_window" is not opening while pressing the button.
-
new_window is created on the stack, not on the heap. As soon as the ctor is left new_window will be automatically destroyed (and thus not shown). new_window will have to be created on the heap.
@
class MyWindow : public QWidget
{
...
MyWindow(QWidget* parent = 0) : QWidget(parent)
{
...
mainWindow = new QMainWindow(this);
...
}
...
private:
QMainWindow* mainWindow;
}
@ -
@
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget* parent = 0, Qt::WindowFlags flags = 0) : QMainWindow(parent, flags) {}protected:
void closeEvent(QCloseEvent* event)
{
emit closed();
QMainWindow::closeEvent(event);
}signals:
void closed();
};class MyWindow : public QWidget
{
Q_OBJECTpublic:
MyWindow(QWidget* parent = 0) : QWidget(parent)
{
...
connect(mainWindow, SIGNAL(closed()), this, SLOT(updateButton()));
}public slots:
void updateButton()
{
button->...
}private:
MainWindow* mainWindow;
};
@
Brain to terminal. Not tested. Exemplary. -
You can handle any event listed "here":http://doc.qt.nokia.com/latest/qwidget.html#protected-functions.