Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt (3) Check QtDesigner and the ui file

Published on| May 22nd, 2009 | No Comment.
Add signals and slots
So, at the click of a button, as you exit the application definition.
Here's an example.

[sample.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "QMessageBox"
#include "ui_test.h"
 
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
	QWidget *widget = new QWidget;
	Ui::Form ui;
	ui.setupUi(widget);
 
	QPushButton* ui_findButton = qFindChild<QPushButton*>(widget, "pushButton");
 
	if(ui_findButton!=NULL){
		widget->connect(ui_findButton, SIGNAL(clicked()), 
		        qApp, SLOT(closeAllWindows()));
	} else {
		QMessageBox::information (NULL,"Nothing","Nothing pushButton.");
	}
 
	widget->show();
	return app.exec();
}

In line 11, from the "pushButton" from the name you will find a widget that QPushButton objects. QPushButton find the signal "clicked", slot "closeAllWindows" is assigned.

In fact, please click Run. Close the screen, I was finished.

Thus, if the signals and slots that already exist, so the screen image to QtDesigner can easily create and control.

However, in most cases is to define a separate slot. When doing so, you must use the metabase. In order to use moc, have a right to recognize inherited class, I'll have separate header and class.

So, QWidget create an inherited class, we have a separate slot.

The sample files in the same way as the previous article to prepare.

  • sample.cpp
    - Main is routine.
  • mainwindow.h
    - QWidget class is inherited header.
  • mainwindow.cpp
    - QWidget inherited class source.

Let's look at the source code.

[mainwindow.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include "ui_test.h"
#include <QtGui>
 
class MainWindow : public QWidget
{
	Q_OBJECT
 
public:
	MainWindow();
 
private slots:
    void on_pushButton_clicked();
 
 
private:
	Ui::Form ui;
};
#endif
Line 15 is defined in the slot.
Please note the name.
"on_widge namet_Signal name" Note that you must have.

Slot name, QtDesigner can also be specified, and if anything not specified in the law of the above, you can define a default slot.
By definition, connect will have to describe the process better.

[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "QMessageBox"
#include "mainwindow.h"
 
MainWindow::MainWindow()
{
	ui.setupUi(this);
 
    setWindowTitle(tr("SampleWindow"));
 
}
 
void MainWindow::on_pushButton_clicked()
{
	QMessageBox::information (NULL,"Click","Click Click!!");
}
6 in line, Like when we used to direct the class was created automatically on the screen and then paste the widget QtDesigne defined.
12 from the line is in operation when the button is clicked.
This is to display a message box.


[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
#include <QApplication>
 
#include "mainwindow.h"
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

Here, I think the explanation is not required. Create a simple application, just make the screen display.

Now, let's run.

As follows: I think the message box.



Here is a simple inheritance is used, Ui:: Form can not be the same as multiple inheritance at the same time. Or as an attribute of the original inheritance (parent) or a difference.


Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ