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 (2) Examine the signals and slots

Published on| May 21st, 2009 | No Comment.
Update Signal and Command Signal
Here, let's create a signal in the slot and click on the menu and menu updates.

For convenience, the command signal is called signal-click menu. Update Signal the signal is called when a menu update.

Qt is a GUI library, so, let's create a sample in the next screen.

Like the three sample preparation.

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

Similarly, QWidget (QObject) class inheritance is sure, you have to separate the header and the source. moc does not recognize correctly.

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
22
23
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QtGui>
 
class MainWindow : public QWidget
{
	Q_OBJECT
 
public:
	MainWindow();
 
private slots:
	void updateMenus();
 
private:
	QMenuBar *m_menu;
	QMenu *m_fileMenu;
	QAction *m_exit_act;
 
	bool m_active;
};
#endif
This time, it creates a menu, QMenuBar, QMenu, QAction is added.
m_active is the state flag to enable the disabled menu item.


[mainwindow.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "mainwindow.h"
 
MainWindow::MainWindow()
{
	m_menu = new QMenuBar( this );
 
	m_fileMenu = m_menu->addMenu(tr("&File"));
 
	m_exit_act = m_fileMenu->addAction("E&xit" );
	m_exit_act->setShortcut(tr("Ctrl+Q"));
	m_exit_act->setStatusTip(tr("Exit the application"));
 
	connect(m_exit_act, SIGNAL(triggered()), 
	         qApp, SLOT(closeAllWindows()));
	connect(m_fileMenu, SIGNAL(aboutToShow()),
	         this, SLOT(updateMenus()));
 
    setWindowTitle(tr("SampleWindow"));
 
	m_active=true;
}
 
void MainWindow::updateMenus()
{
	m_exit_act->setEnabled(m_active);
	m_active=!m_active;
}
In line 5, to create a menu bar. Widget is to manage the entire menu.
In line 7, the menu bar "File" and add a pop-up menu.
9 from the line, "File" pop-up menu called "Exit" menu item to add it.

The past, MFC in the same way as the ID was to manage and, now, is to manage action items. But it is no longer ID, this is to manage action items along with the flow.

From MFC, VCL or feel close to you. VCL using C + + but I did like this.

Line 13, the menu "Exit" when I click on, qApp of "closeAllWindows" I have to start the method declaration.
In other words, defines a Command Signal and its corresponding slot.
Line 15, the pop-up menu "File" when you see the object (this) of "updateMenus" I have to start the method declaration.
In other words, defines the slot and its corresponding Update Signal.

The qApp, MFC within-AfxGetApp () is similar.
MFC is not the only CWinApp CWinApp or after generation inherited objects, objects取RI出SEMASEN correctly.
Qt is the same. It corresponds to the QApplication. Without this reality, qApp is empty.
Follows in the main, the first of which creates a QApplication is also for that.

23 from the line, pop-up menu "File" menu item is displayed each time the "Exit" to disable and enable the toggle switch.

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

The following menu, "Exit" I think every time you see the switch disabled.



Definition of each signal, so all are included in the reference class is good in there.
Most of the necessary signals, and we are aligned. Can easily be created as described above if the individual signals.

Qt class reference: http://doc.trolltech.com/4.5/classes.html


Comments

Leave a Reply







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