[Solved] Qt creator Project using qt designer
-
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.
-
You can use the debugger as well to trace and track.
-
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_OBJECTpublic:
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", "<html><head/><body><p><span style=\" color:#aa00ff;\">I/O Card2</span></p></body></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", "<html><head/><body><p><span style=\" color:#aa007f;\">I/O Card1</span></p></body></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 UiQT_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_OBJECTpublic:
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", "<html><head/><body><p><span style=\" font-size:12pt; color:#55aa00;\">Enter Channel Number</span></p></body></html>", 0, QApplication::UnicodeUTF8)); Select->setText(QApplication::translate("window1", "Select", 0, QApplication::UnicodeUTF8)); } // retranslateUi
};
namespace Ui {
class window1: public Ui_window1 {};
} // namespace UiQT_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 codingSorry 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/editwindow1
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/editusing 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 alwaysthanks a lot for your reply to my questions
-
Nice.
Please mark the thread as "Solved"