PDA

View Full Version : Need help with button !



wreckx
22nd November 2010, 09:00
Hi everyone,

after get a lots of help from expert im still stuck with my button..

i have 2 "design" page (form.ui) "Qdialog"

mainwindow.ui

and

dialog.ui


i have one button on mainwindow.ui and i want to click that button to go to dialog.ui..

i design only with the designer thing with a double click on mainwindow.ui or dialog.ui

can someone please write me that simple step for me ?? i really can't figure out how and where to write codes - -" ... as you will see i put some script already (from the help i had in my last post) the debug mode works fine but when i press the button my program crash ?!!

i guess something wrong with my code still or im not doing the step right..

thank you very much

Max




here are the code from each pages:

Dialog.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QCalendarWidget" name="calendarWidget">
<property name="geometry">
<rect>
<x>80</x>
<y>40</y>
<width>256</width>
<height>155</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>



mainwindow.ui


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>360</width>
<height>640</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>120</x>
<y>180</y>
<width>121</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>



dialog.h


#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

private:
Ui::Dialog *ui;
};

#endif // DIALOG_H



mainwindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QDialog>
#include "dialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{

QDialog *dialog;
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

private slots:
void on_pushButton_clicked();
};

#endif // MAINWINDOW_H



dialog.ccp



#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}



main.cpp



#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;

#if defined(Q_WS_S60)
w.showMaximized();
#else
w.showFullScreen();

#endif
return a.exec();
}



mainwindow.cpp



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_dialog.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
dialog->exec();
}

Zlatomir
22nd November 2010, 09:07
You have only the pointer there, create an object, using new if you use a pointer, or you can create one dialog on the stack if you use .exec() to make it modal.

Take a look at this (http://www.qtcentre.org/faq.php?faq=qt_general_category#faq_qt_designer_2f orms) FAQ

LE: also check the classes names, because you created your own Dialog class and you created a pointer to QDialog class in the mainwindow.

wreckx
22nd November 2010, 10:09
Hello Zlatomir,

thank you for your fast reply..

and thank you for the FAQ its exactly what i need to do ... ;)

hopefully i will be able to change from my .ui to my second .ui !! finally - -"

Added after 58 minutes:

Ok i try the exemple and it work perfectly ... i push the button and it goes to the page2 ..

the probleme is my second page is blank ?? it didnt call my (page2.UI) but create a new page2 i guess ??

do i have to declared that is a .UI page ??? if so how can i set that up ?


the script i use :

void mainwindow::openPage2()
{
Page2->show();
Page2->activateWindow();
Page2->raise();
}


thank you

Max

Zlatomir
22nd November 2010, 10:47
What type is Page2?

As i see you have created the Dialog class in dialog.h and you want that to open, don't you?

Just include that header in the mainwindow .cpp file (the one in with you define the slot)
and write:



#include "dialog.h" // or include the name_of_your_dialog_class

void mainwindow:OpenPage2()
{
Dialog Page2(this);
Page2.exec();
}

Or if you want it non-modal you declare the Page2 a Dialog pointer (Dialog *Page2; ) member in the mainwindow.h, and can include the dialog.h, or better use forward declaration and include only in the mainwindow.cpp.
Then in the slot you write something like:

Page2 = new Dialog(this);
Page2->show();

wreckx
22nd November 2010, 10:52
Page2="dialog.ui"

and that a Qdesigner Form Class "Qdialog with button" (Form folder) and the extension is .ui

and the dialog.h come when i create the new Qdesigner class..

im gonna try your script straight away and back with the result

thank you very much for all your help

Max

Zlatomir
22nd November 2010, 11:01
...
and the dialog.h come when i create the new Qdesigner class..

When you runned the Wizzard that created the ui form, it created the C++ class that "include" the UIC's xml to C++ "translation", otherwise you whould have needed to create that class in one of those (http://doc.qt.nokia.com/4.7/designer-using-a-ui-file.html) methods.

The important thing (since that C++ class is generated) is to look at the generated code and see the name of the class that is "including" the Ui that you want to show as a dialog.

wreckx
22nd November 2010, 11:08
i try the 2 exemples and still get a Blank page ...

i really don't understand why ?? for try i just put a calendar on my dialog.ui .. the calendar don't appear .. just a blank new page..

any idea??

Max



edit:: i manage to make it work

in my mainwindow.cpp :

void MainWindow:: on_Button1_clicked()
{
Dialog p;
p.exec();
}



and that it .. it open my second .UI .. !!

i spend a week on that and the script is just 2 ridiculous ligne - -" ...

Zlatomir
22nd November 2010, 11:17
You changed the names of the slots from void on_pushButton_clicked(); to void OpenPage2(); make sure that you are not still calling some older code that didn't work.

Only the first "naming_version" is auto-connected with the signal clicked of the pushButton, the second name must be connected manually by you.

I really suggest you make all the connections yourself at least until you get comfortable with the "pattern", read more on signal-slot mechanism (http://doc.qt.nokia.com/4.7/signalsandslots.html)

wreckx
22nd November 2010, 11:21
thank you a lots Zlatomir for the time you spend on replying !!

i finally make it work as you see in my last post..

Thank you very much

Sincerely,

Max