Problem in displaying window?
-
wrote on 19 Sept 2011, 10:42 last edited by
If i am creating a window inside another window class constructor, then why the window is not visible while i am using function window.show().
-
wrote on 19 Sept 2011, 10:56 last edited by
Could you be a bit more elaborate? How are you construting the "windows", and what makes you think something will be shown just because you are creating it in a constructor?
If I understand you correctly, you have two widgets a (of class A) and b (of class B), and you create b inside the constructor of A? Then you call a.show(). And you excpect to be able to see b inside a?
This should normally be the case, provided that you also made a b child of a, e.g. by passing a as the parent argument of B's constructor when you create b.
-
wrote on 19 Sept 2011, 10:57 last edited by
You could have been a little more specific. Whether you are using Dialogs or Mainwindow or whatever.. Or your code.
[oops, not timely posted.. Bit late ;)]
-
wrote on 19 Sept 2011, 11:02 last edited by
ya i am creating inside the constructor using QMainwindow class.
-
wrote on 19 Sept 2011, 13:21 last edited by
A QMainWindow has a central widget, which you set using the setCentralWidget() function, and dock windows, which you can add using addDockWidget(). Did you use one of those functions to add your widget to the main window?
-
wrote on 19 Sept 2011, 13:50 last edited by
You will have to provide some sample code (at least your ctor). Everything else is just groping in the dark.
-
wrote on 20 Sept 2011, 04:04 last edited by
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.
-
wrote on 20 Sept 2011, 05:27 last edited by
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;
}
@ -
wrote on 20 Sept 2011, 05:52 last edited by
thank's for the suggestion. In this program i want to notify button when window is closed how can i do it?
-
wrote on 20 Sept 2011, 06:05 last edited by
Emit a signal in the QMainWindow::closeEvent() (by subclassing or using an event filter), which is connected to a slot in your MyWindow class. Use this slot to modify the button.
-
wrote on 20 Sept 2011, 06:33 last edited by
can you show that by writing an example
-
wrote on 20 Sept 2011, 07:41 last edited by
@
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. -
wrote on 20 Sept 2011, 09:52 last edited by
thanks it is working. can we control mouse press and release event also like this?
-
wrote on 20 Sept 2011, 10:32 last edited by
You can handle any event listed "here":http://doc.qt.nokia.com/latest/qwidget.html#protected-functions.
1/14