[Solved] Qt creator Project using qt designer



  • hiii every one,

    I am new to GUI programming in Linux. So please help me

    I am started Qt widget based Gui project in Qt creator. By opening this main.cpp, mainwindow.cpp, mainwindow.h & mainwindow.ui files are created in project file.
    And I added 2 more widget based files--
    for 1st form, form1.cpp,form1.h,form1.ui
    & for 2nd form, form1.cpp,form1.h,form1.ui files are created.

    my question is--

    1st I simply made a program like by clicking push button In main window it will go to 1st form but when i clicking a push button in 1st form it does not went to 2nd form. what is the problem? is there any function calls required?



  • Could you show the code ?



  • In main.cpp
    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.setWindowTitle("Project");
    w.size();
    w.resize(400,250);
    return a.exec();
    }
    @

    In minwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

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

    }

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

    void MainWindow::on_Card1_Read_clicked()
    {

    QWidget *widget =new QWidget;
    Ui::Form1 Read1;
    Read1.setupUi(widget);
    widget->show();
    

    }

    void MainWindow::on_Card2_write_clicked()
    {
    QWidget *widget =new QWidget;
    Ui::Form2 Write1;
    Write1.setupUi(widget);
    widget->show();
    }
    @

    form1.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<ui_form2.h>
    #include<form2.h>
    #include<ui_form1.h>
    #include<form1.h>

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

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

    void Form1::on_pushButton_clicked()
    {
    Qmsgbx.setText("hai");
    Qmsgbx.show();
    }
    @

    when i run this program mainwindow will appears and by clicking any one pushbutton on mainwindow it will dispaly the form1 or form2 depends on pushbutton. up to this its fine but when i click the push button on form1 window it will not dispaly the msg(here am taking just display the msg).

    or if i write a code to go for next window in void Form1::on_pushButton_clicked() this function it doesn't give any output.
    please help me



  • I think you should start trying with the examples.

    And go through the Object Tree basics, Signals and Slots.

    Also, If you mean to use a multi window application, "QDockWidget":http://qt-project.org/doc/qt-4.8/qdockwidget.html would be a nice place to start.



  • thanx for the reply.

    I am using QT designer, in designer form we are drag & drop the labels, from that label we can directly go to slots, so there is no need of signals, Am I right?

    In my code main window & forms having parent as 0.
    Is there any modification for that Q Widget parent value in the form1 & form2? If it is please explain something about this.

    If parent=0 means that widget doesn't have parent so its a window. In my project i want window accessing only but it is from 2nd window to third window
    Is there any wrong in my understand? if so please explain me



  • You can use Designer to Configure signal and Slots of a widget. [ Key "F4" leads to that view, and i guess, you may have used this so far.] And there is always a need of signals !! Here is "How to":http://qt-project.org/doc/qt-4.8/designer-connection-mode.html

    Parent as 0 means, no parent!

    Instead of the Direct Approach, try the above way, that is lot easier.



  • Hai,

    Configuring signal & slots from designer also tried but there is no response to do next activity.
    In Gui designer I tried this signal & slot mechanism for LineEdit to pushbutton.

    The signal for lineEdit is textEdited() and slot for pushbutton is clicked() function but there is no change.
    I have doubt regarding this,,, If we use signal & slot configuration from designer, code in slot() function needed? If it is then where can I write the code?

    I tried the code to exit the window, even it is also not executed

    to exit I written the code in pushbutton_clicked() slot, as exit(0) or close(). In main window it's excuted but when i write the same code in 2nd window It's not .

    I didn't understand where I get problem. Is there any limitation to use windows?

    Other wise can you give me some example code for multiple windows(4 to 5)



  • You may use Designer, or Code directly, its almost same. you can see the 'connect' in ui_somename.h of corresponding somename.ui file. UIC does this job.

    You have to read "signals & Slots":http://qt-project.org/doc/qt-4.8/signalsandslots.html bit thoroughly to get the clear picture, which is the very basic and fundamental of Qt.

    And , regarding the doubt, Yes, you need to define and declare the slot.

    Example, If you are using MainWindow.ui where you are connecting a push button or widget to slot1(), And you may include ui_MainWindow.h in MainWindow.cpp, the just declare your slot as any member function in MainWindow.h and Define in MainWindow.cpp. And you do your stuff inside the slot.

    When you create a widget with no parent, it will come in new window. And if you want to create another window, in the very same way, from the newly created window, (in your case Frame 2 from Frame1) you have to create the slot in Form1.



  • Thanx sir,

    I will try this first then I will reply to you

    In mainwindow.cpp i write the code to go to next window form1 by clicking a pushbutton in mainwindow as shown in above code,
    same way I written the code in form1.cpp to go next window form2 using pushbutton slot, but it is not taken that signal. Can I change any thing in Ui_form1.h file



  • Sir Am tried one example by creating another project.

    In the .ui file Am added one lineedit & pushbutton

    Using Signals & slots I am connected these two through ui only

    In editing these signals I am selected editText(Qstring) signal for lineEdit & click() slot for pushbutton and I written the code for both functions .cpp file but writting something in lineEdit its not giving the display using QMessageBox(in slot function this code is written)



  • You can change in ui_*.h file, but not usually needed at all!

    Click() is not a slot, its a signal.

    Its a simple logic. One of your Widget emits signal, and a receiver catches that and does some [According to the Connection].

    Now when you connect the signal to the slot, using the designer, same things are happening, and the QObject::connect(.... statement can be found in ui_...h,for your clarification, which is generated by UIC.

    Now, in the case you told about a push button and Lineedit, check the following things.

    1.You have connected Pushbutton signal "Clicked" to your Slot, say slot1().

    2.Make sure you declared the slot1() and defined correctly.

    3.In slot1(), you update the text of lineedit etc.

    1. You can use the debugger as well to trace and track.

    2. Go through this "previous thread":http://qt-project.org/forums/viewthread/816 and "This Sample":http://doc.qt.digia.com/qtcreator/creator-writing-program.html. I guess all your answers are there.



  • Thanks alot,

    Mostly all of my doubts are cleared now, but
    one thing left.

    which ever I used in mainwindow program, same thing will be used in form1(.ui file & .cpp file & .h file)
    I am linking the form1 to form2 till it is not cleared I am strucked here it self



  • Sir I am posted my total project,,

    Check it out once and find the Where i get the problem.
    Main.cpp
    @

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

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.setWindowTitle("IFSS");
    w.size();
    w.resize(400,250);
    return a.exec();
    }

    @

    Mainwindow.cpp
    @

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "window1.h"
    #include "ui_window1.h"
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    void MainWindow::on_Read1_clicked()
    {
    QMainWindow *w1 =new QMainWindow();
    Ui::window1 Read1;
    Read1.setupUi(w1);
    w1->show();
    w1->resize(550,150);
    }
    void MainWindow::on_Write1_clicked()
    {
    //code to be this button
    }

    void MainWindow::on_Read2_clicked()
    {
    //code to be this button
    }
    void MainWindow::on_Write2_clicked()
    {
    msgbox.setText("Card2 is writting");
    msgbox.show();
    }

    @

    mainwindow.h

    @

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "window1.h"
    #include "ui_window1.h"
    #include "QMessageBox"
    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private slots:

    void on_Read1_clicked();
    
    void on_Write1_clicked();
    
    void on_Read2_clicked();
    
    void on_Write2_clicked();
    

    private:
    Ui::MainWindow *ui;
    QMessageBox msgbox;

    };

    #endif // MAINWINDOW_H
    @
    total code will be in next post



  • ui_mainwindow.h file

    @
    #ifndef UI_MAINWINDOW_H
    #define UI_MAINWINDOW_H

    #include <QtCore/QVariant>
    #include <QtGui/QAction>
    #include <QtGui/QApplication>
    #include <QtGui/QButtonGroup>
    #include <QtGui/QHBoxLayout>
    #include <QtGui/QHeaderView>
    #include <QtGui/QLabel>
    #include <QtGui/QMainWindow>
    #include <QtGui/QMenuBar>
    #include <QtGui/QPushButton>
    #include <QtGui/QStatusBar>
    #include <QtGui/QToolBar>
    #include <QtGui/QWidget>

    QT_BEGIN_NAMESPACE

    class Ui_MainWindow
    {
    public:
    QWidget *centralWidget;
    QWidget *layoutWidget;
    QHBoxLayout *HL1;
    QLabel *Card2;
    QPushButton *Read2;
    QPushButton *Write2;
    QWidget *layoutWidget1;
    QHBoxLayout *HL;
    QLabel *Card1;
    QPushButton *Read1;
    QPushButton *Write1;
    QMenuBar *menuBar;
    QToolBar *mainToolBar;
    QStatusBar *statusBar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(400, 300);
        centralWidget = new QWidget(MainWindow);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        layoutWidget = new QWidget(centralWidget);
        layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
        layoutWidget->setGeometry(QRect(20, 130, 271, 29));
        HL1 = new QHBoxLayout(layoutWidget);
        HL1->setSpacing(6);
        HL1->setContentsMargins(11, 11, 11, 11);
        HL1->setObjectName(QString::fromUtf8("HL1"));
        HL1->setContentsMargins(2, 0, 0, 0);
        Card2 = new QLabel(layoutWidget);
        Card2->setObjectName(QString::fromUtf8("Card2"));
    
        HL1->addWidget(Card2);
    
        Read2 = new QPushButton(layoutWidget);
        Read2->setObjectName(QString::fromUtf8("Read2"));
    
        HL1->addWidget(Read2);
    
        Write2 = new QPushButton(layoutWidget);
        Write2->setObjectName(QString::fromUtf8("Write2"));
    
        HL1->addWidget(Write2);
    
        layoutWidget1 = new QWidget(centralWidget);
        layoutWidget1->setObjectName(QString::fromUtf8("layoutWidget1"));
        layoutWidget1->setGeometry(QRect(20, 50, 274, 29));
        HL = new QHBoxLayout(layoutWidget1);
        HL->setSpacing(6);
        HL->setContentsMargins(11, 11, 11, 11);
        HL->setObjectName(QString::fromUtf8("HL"));
        HL->setContentsMargins(2, 0, 0, 0);
        Card1 = new QLabel(layoutWidget1);
        Card1->setObjectName(QString::fromUtf8("Card1"));
    
        HL->addWidget(Card1);
    
        Read1 = new QPushButton(layoutWidget1);
        Read1->setObjectName(QString::fromUtf8("Read1"));
    
        HL->addWidget(Read1);
    
        Write1 = new QPushButton(layoutWidget1);
        Write1->setObjectName(QString::fromUtf8("Write1"));
    
        HL->addWidget(Write1);
    
        MainWindow->setCentralWidget(centralWidget);
        menuBar = new QMenuBar(MainWindow);
        menuBar->setObjectName(QString::fromUtf8("menuBar"));
        menuBar->setGeometry(QRect(0, 0, 400, 25));
        MainWindow->setMenuBar(menuBar);
        mainToolBar = new QToolBar(MainWindow);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
        statusBar = new QStatusBar(MainWindow);
        statusBar->setObjectName(QString::fromUtf8("statusBar"));
        MainWindow->setStatusBar(statusBar);
    
        retranslateUi(MainWindow);
    
        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi
    
    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
        Card2->setText(QApplication::translate("MainWindow", "&lt;html&gt;&lt;head/>&lt;body&gt;&lt;p><span style=\" color:#aa00ff;\">I/O Card2</span></p>&lt;/body&gt;&lt;/html>", 0, QApplication::UnicodeUTF8));
        Read2->setText(QApplication::translate("MainWindow", "Read", 0, QApplication::UnicodeUTF8));
        Write2->setText(QApplication::translate("MainWindow", "Write", 0, QApplication::UnicodeUTF8));
        Card1->setText(QApplication::translate("MainWindow", "&lt;html&gt;&lt;head/>&lt;body&gt;&lt;p><span style=\" color:#aa007f;\">I/O Card1</span></p>&lt;/body&gt;&lt;/html>", 0, QApplication::UnicodeUTF8));
        Read1->setText(QApplication::translate("MainWindow", "Read", 0, QApplication::UnicodeUTF8));
        Write1->setText(QApplication::translate("MainWindow", "Write", 0, QApplication::UnicodeUTF8));
    } // retranslateUi
    

    };

    namespace Ui {
    class MainWindow: public Ui_MainWindow {};
    } // namespace Ui

    QT_END_NAMESPACE

    #endif // UI_MAINWINDOW_H

    @

    Up to this is main program.

    now first form--named window1

    window1.h

    @
    #ifndef WINDOW1_H
    #define WINDOW1_H

    #include <QMainWindow>
    #include<QMessageBox>
    namespace Ui {
    class window1;
    }

    class window1 : public QMainWindow
    {
    Q_OBJECT

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

    private slots:
    void on_Select_clicked();

    private:
    Ui::window1 *ui1;
    QMessageBox msgbox;
    };

    #endif // WINDOW1_H
    @

    window1.cpp

    @
    #include "window1.h"
    #include "ui_window1.h"

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

    window1::~window1()
    {
    delete ui1;
    }

    void window1::on_Select_clicked()
    {
    QString str =ui1->Channel_Number->text();
    int n=str.toInt();

        if(n==1)
        {
        msgbox.setText("Number1");
        msgbox.show();
        }
        if(n==2)
        {
        msgbox.setText("Number2");
        msgbox.show();
        }
        if(n==3)
        {
        msgbox.setText("Number3");
        msgbox.show();
        }
    

    }
    total code will be post in next one



  • @

    & ui_window1.h

    @/********************************************************************************
    ** Form generated from reading UI file 'window1.ui'
    **
    ** Created: Tue May 21 16:00:46 2013
    ** by: Qt User Interface Compiler version 4.8.1
    **
    ** WARNING! All changes made in this file will be lost when recompiling UI file!
    ********************************************************************************/

    #ifndef UI_WINDOW1_H
    #define UI_WINDOW1_H

    #include <QtCore/QVariant>
    #include <QtGui/QAction>
    #include <QtGui/QApplication>
    #include <QtGui/QButtonGroup>
    #include <QtGui/QHeaderView>
    #include <QtGui/QLabel>
    #include <QtGui/QLineEdit>
    #include <QtGui/QMainWindow>
    #include <QtGui/QMenuBar>
    #include <QtGui/QPushButton>
    #include <QtGui/QStatusBar>
    #include <QtGui/QWidget>

    QT_BEGIN_NAMESPACE

    class Ui_window1
    {
    public:
    QWidget *centralwidget;
    QLabel *Channel;
    QLineEdit *Channel_Number;
    QPushButton *Select;
    QMenuBar *Window1;
    QStatusBar *statusbar;

    void setupUi(QMainWindow *window1)
    {
        if (window1->objectName().isEmpty())
            window1->setObjectName(QString::fromUtf8("window1"));
        window1->resize(800, 600);
        centralwidget = new QWidget(window1);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        Channel = new QLabel(centralwidget);
        Channel->setObjectName(QString::fromUtf8("Channel"));
        Channel->setGeometry(QRect(30, 70, 171, 17));
        Channel_Number = new QLineEdit(centralwidget);
        Channel_Number->setObjectName(QString::fromUtf8("Channel_Number"));
        Channel_Number->setGeometry(QRect(220, 70, 113, 27));
        Select = new QPushButton(centralwidget);
        Select->setObjectName(QString::fromUtf8("Select"));
        Select->setGeometry(QRect(360, 70, 98, 27));
        window1->setCentralWidget(centralwidget);
        Window1 = new QMenuBar(window1);
        Window1->setObjectName(QString::fromUtf8("Window1"));
        Window1->setGeometry(QRect(0, 0, 800, 25));
        window1->setMenuBar(Window1);
        statusbar = new QStatusBar(window1);
        statusbar->setObjectName(QString::fromUtf8("statusbar"));
        window1->setStatusBar(statusbar);
    
        retranslateUi(window1);
    
        QMetaObject::connectSlotsByName(window1);
    } // setupUi
    
    void retranslateUi(QMainWindow *window1)
    {
        window1->setWindowTitle(QApplication::translate("window1", "MainWindow", 0, QApplication::UnicodeUTF8));
        Channel->setText(QApplication::translate("window1", "&lt;html&gt;&lt;head/>&lt;body&gt;&lt;p><span style=\" font-size:12pt; color:#55aa00;\">Enter Channel Number</span></p>&lt;/body&gt;&lt;/html>", 0, QApplication::UnicodeUTF8));
        Select->setText(QApplication::translate("window1", "Select", 0, QApplication::UnicodeUTF8));
    } // retranslateUi
    

    };

    namespace Ui {
    class window1: public Ui_window1 {};
    } // namespace Ui

    QT_END_NAMESPACE

    #endif // UI_WINDOW1_H

    @

    When i run this project mainwindow will displaying the window1 but when I am clicking pushbutton in window1 it will not display the entered value....even also

    is there any wrong in my code? please help me now I have less time to complete this project
    completion of this one only I move on to further coding

    Sorry for this much of code please excuse me



  • You are using [[doc:QMainWindow]], instead of Simple [[doc:QDialog]]

    If you provide me the ui files source, it would be easier for me to go through than the generated code.

    You may click the read 1 as many times you want, that many sub windows will be there. But write2 is correct. You may correct this, by replacing the new MainWindow with any other widget.

    Sorry, even i have time constrains for a line by line check and execution.



  • this is main window
    https://docs.google.com/file/d/0B_84hMr4KHFdX2lmcXJBSWpQdnc/edit

    window1

    https://docs.google.com/file/d/0B_84hMr4KHFdWHZFWFB0WnBOS2c/edit

    and I am tried using dilaog widget also.
    dialog1
    https://docs.google.com/file/d/0B_84hMr4KHFdWHZFWFB0WnBOS2c/edit

    using dialog also am getting same.

    If I use the same signal & slot as dialog, the window will closed. but I want to execute something.



  • hai

    my problem was solved.

    to go another window like in my code from mainwindow to next window1
    I am used
    @
    QMainWindow *w1 =new QMainWindow();
    Ui::window1 Read1;
    Read1.setupUi(w1);
    w1->show();
    @

    instead of this I am tried

    @
    window1 *w=new window1();
    w->show();
    @

    I got solution.

    1st code is useful for only main window to next form its not for form1 to form2
    2nd code useful for always

    thanks a lot for your reply to my questions



  • Nice.

    Please mark the thread as "Solved"


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.