Results 1 to 9 of 9

Thread: Need help with button !

  1. #1
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Need help with button !

    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();
    }
    Last edited by wreckx; 22nd November 2010 at 09:03. Reason: reformatted to look better

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help with button !

    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 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.

  3. #3
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need help with button !

    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:penPage2()
    {
    Page2->show();
    Page2->activateWindow();
    Page2->raise();
    }


    thank you

    Max
    Last edited by wreckx; 22nd November 2010 at 10:41. Reason: spelling corrections

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help with button !

    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:

    Qt Code:
    1. #include "dialog.h" // or include the name_of_your_dialog_class
    2.  
    3. void mainwindow:OpenPage2()
    4. {
    5. Dialog Page2(this);
    6. Page2.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. Page2 = new Dialog(this);
    2. Page2->show();
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to Zlatomir for this useful post:

    wreckx (22nd November 2010)

  6. #5
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need help with button !

    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
    Last edited by wreckx; 22nd November 2010 at 10:53. Reason: updated contents

  7. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help with button !

    Quote Originally Posted by wreckx View Post
    ...
    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 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.

  8. The following user says thank you to Zlatomir for this useful post:

    wreckx (22nd November 2010)

  9. #7
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need help with button !

    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 - -" ...
    Last edited by wreckx; 22nd November 2010 at 11:21. Reason: updated contents

  10. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need help with button !

    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

  11. The following user says thank you to Zlatomir for this useful post:

    wreckx (22nd November 2010)

  12. #9
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need help with button !

    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

Similar Threads

  1. Changing text of button in no relation to button
    By Sabre Runner in forum Newbie
    Replies: 22
    Last Post: 23rd September 2010, 12:29
  2. Replies: 6
    Last Post: 21st August 2010, 21:09
  3. Replies: 1
    Last Post: 2nd August 2010, 05:40
  4. Custom widget: a button within a button
    By Gnurou in forum Qt Programming
    Replies: 7
    Last Post: 18th June 2009, 09:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.