PDA

View Full Version : send signal from 1 gui to another gui



tho97
5th December 2007, 20:46
Hi,

I try to send a signal from my main gui window (when it closes) to another gui ( which start that main window gui). How I can do it?
I got a segment fault when I try this code
main window header:



#include "ui_dirviewdialog.h"
class MainWindow : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT

public:
MainWindow(QMainWindow *parent = 0);

signals:
void close();

protected:
void closeEvent(QCloseEvent *event);


my main cpp code:



MainWindow::MainWindow(QMainWindow *parent)
: QMainWindow(parent)
{
setupUi(this);
}
void MainWindow::closeEvent(QCloseEvent *event ){
emit close();
event->accept();
}


gui that start that main window:


#include "ui_serverdialog.h"
class MainWindow;

class ServerDialog : public QDialog, public Ui::ServerDialog
{
Q_OBJECT

public:
ServerDialog(QWidget *parent = 0);

private slots:
void gui();
void stopgui();

private:
MainWindow *mainWindow;
};


cpp file:



#include "serverdialog.h"
#include "dirviewdialog.h"


ServerDialog::ServerDialog(QWidget *parent)
: QDialog(parent, Qt::WindowMinimizeButtonHint)
{
QWidget::setWindowFlags(Qt::WindowMinimizeButtonHi nt);
setupUi(this);
stopButton->setEnabled(false);
stopguiButton->setEnabled(false);

connect(runButton, SIGNAL(clicked()), this, SLOT(gui()));
connect (mainWindow, SIGNAL(close()), this, SLOT(stopgui()));
}
void ServerDialog::gui()
{

mainWindow = new MainWindow;
mainWindow->showMaximized();
mainWindow->activateWindow();
runButton->setEnabled(false);
stopguiButton->setEnabled(true);
}

void ServerDialog::stopgui()
{
if(mainWindow != 0){
delete mainWindow;
}
runButton->setEnabled(true);
stopguiButton->setEnabled(false);
}

jacek
5th December 2007, 21:01
connect (mainWindow, SIGNAL(close()), this, SLOT(stopgui()));
You don't initialize mainWindow. Another problem is that you should create that connection for every new MainWindow object you create. So the easiest solution is to move this line to ServerDialog::gui().

marcel
5th December 2007, 21:02
You create the mainWindow object after you connect it to your slot. So at the time of connect, you have an invalid pointer there.
Try either creating the object before connect, or moving the connect in the function where you create the window.

tho97
5th December 2007, 22:21
thank you, it solved my segment fault and work now after I move connect to gui()