Вы находитесь на странице: 1из 21

2. .

-- Qt .
, ()
.
- , - ,
. ,
, ,
,
.
,
, .
.
-.
"",
. ,
, Qt Designer.
,

.
2.1. QDialog.
-- . ,
.
.

2.1. Linux (KDE).

: finddialog.h
finddialog.cpp. finddialog.h.
1
2
3
4
5
6

#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <qdialog.h>
class QCheckBox;
class QLabel;
class QLineEdit;

7 class QPushButton;
1 2 ( 27) -.
3 QDialog -- Qt.
QDialog QWidget.
4 7 -- Qt,
. C++ ,
, (
) . .
FindDialog, QDialog:
8 class FindDialog : public QDialog
9 {
10
Q_OBJECT
11 public:
12
FindDialog(QWidget *parent = 0, const char *name = 0);
Q_OBJECT.
, .
, Qt, -- FindDialog.
parent "", .. , ,
name -- () . name ,
.
13 signals:
14
void findNext(const QString &str, bool caseSensitive);
15
void findPrev(const QString &str, bool caseSensitive);
signals , ,
Find. "Search backward" (
), findPrev(), -- findNext().
signals, . C++
C++
.
16
17
18
19
20
21
22
23
24
25
26
27

private slots:
void findClicked();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif

.
, .
slots, signals, .
- -- ,
, .
, , .

(<qcheckbox.h>, <qlabel.h> ..),
. ,
.
finddialog.cpp,
FindDialog:
1
2
3
4
5

#include
#include
#include
#include
#include

<qcheckbox.h>
<qlabel.h>
<qlayout.h>
<qlineedit.h>
<qpushbutton.h>

6 #include "finddialog.h"
Qt.
6 . Qt,
, (
) .h.
7 FindDialog::FindDialog(QWidget *parent, const char *name)
8
: QDialog(parent, name)
9 {
10
setCaption(tr("Find"));
11
label = new QLabel(tr("Find &what:"), this);
12
lineEdit = new QLineEdit(this);
13
label->setBuddy(lineEdit);
14
caseCheckBox = new QCheckBox(tr("Match &case"), this);
15
backwardCheckBox = new QCheckBox(tr("Search &backward"),
this);
16
findButton = new QPushButton(tr("&Find"), this);
17
findButton->setDefault(true);
18
findButton->setEnabled(false);
19
closeButton = new QPushButton(tr("Close"), this);
8, parent name.
10 , -- "Find".
tr() QObject ,
Q_OBJECT. ,
, . ,
, , ,
, .
15.

, 11, .
('&') (). ,
16 Find,
Alt+F. :
11 , (Alt+W), 13
"" (buddy) -- . ""
-- ,
. ,
Alt+W ( ),
"" , .. -- .
17, setDefault(true) [1] , . - -- ,
Enter. 18 Find.
,
.
20 connect(lineEdit, SIGNAL(textChanged(const QString &)),
21
this, SLOT(enableFindButton(const QString &)));
22 connect(findButton, SIGNAL(clicked()),
23
this, SLOT(findClicked()));
24 connect(closeButton, SIGNAL(clicked()),
25
this, SLOT(close()));
1234
enableFindButton(const QString &)
. findClicked() ,
Find.
Close. close() QWidget -- . findClicked()
enableFindButton(const QString &) .
QObject FindDialog,
QObject:: connect().
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 }

QHBoxLayout *topLeftLayout = new QHBoxLayout;


topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch(1);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setMargin(11);
mainLayout->setSpacing(6);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);


. (layout manager) -- ,
. Qt
: QHBoxLayout ,
QVBoxLayout -- QGridLayouts -- .
(, , ) ,
. QHBoxLayout, QVBoxLayout
QGridLayouts, ,
.

2.2. Find.

QHBoxLayout QVBoxLayout,
2.2. (mainLayout) ,
, ,
FindDialog -- (this).
. 3
. "", - (spacer). Find Close,
.
. .
QLayout, QObject.
, , - , , .
, ( ) .
( ),
, .
"", , .
( ),
mainLayout,
. ( : topLeftLayout,

leftLayout rightLayout), ,
addLayout().
"-" QObject,
QWidget, QLayout. (,
- ), ,
.
,
. ,
, ,
, .
, "".
, -- ,
new, .
, Qt .
.
, . -
, , .
,
addLayout(),
. ,
, addWidget(), .
2.3 "-" Find.
FindDialog,
, new addLayout(). :
, ,
.

2.3. "-" Find.

, Qt
: QHBox ( 1), QVBox QGrid.
,
.
, , .
FindDialog.

new, , , ,
, ,
. , Qt
, .. FindDialog.
:
43 void FindDialog::findClicked()
44 {
45
QString text = lineEdit->text();
46
bool caseSensitive = caseCheckBox->isOn();
47
48
49
50
51 }

if (backwardCheckBox->isOn())
emit findPrev(text, caseSensitive);
else
emit findNext(text, caseSensitive);

52 void FindDialog::enableFindButton(const QString &text)


53 {
54
findButton->setEnabled(!text.isEmpty());
55 }
findClicked() ,
Find. findPrev() findNext(),
Search backward. emit ,
Qt.
enableFindButton() ,
. - , Find,
.
.
main.cpp,
:
1 #include <qapplication.h>
2 #include "finddialog.h"

3 int main(int argc, char *argv[])


4 {
5
QApplication app(argc, argv);
6
FindDialog *dialog = new FindDialog;
7
app.setMainWidget(dialog);
8
dialog->show();
9
return app.exec();
10 }
qmake, . , FindDialog
Q_OBJECT, qmake Makefile
, moc -- .
moc ,
, . ,
moc .
, Q_OBJECT,
. ,
, qmake
Makefile. Makefile,
"" .
. , GCC :
finddialog.o(.text+0x28): undefined reference to
FindDialog::QPaintDevice virtual table
Visual C++ :
finddialog.obj : error LNK2001: unresolved external symbol
"public:~virtual bool __thiscall FindDialog::qt_property(int,
int,class QVariant *)"
, qmake , Makefile,
.
. Alt+W, Alt+C, Alt+B
Alt+F. "" Tab. -,
Tab, ,
.
QWidget::setTabOrder().
, Tab,
, .
.
3 .
findPrev() findNext() .

[1] Qt : TRUE
FALSE, true false.

, , Qt,
, true
false.
////////////////////////////////////////////////////////////////////////////////////////////////////

2.2. .
Qt.
, .
,
, .
.
, - C++.
, ,
, ,
-. ,
. , - ,
.
connect() :
connect(sender, SIGNAL(signal), receiver, SLOT(slot));

sender receiver -- QObject ( ),


signal slot -- . SIGNAL() SLOT()
.
. .

:
connect(slider, SIGNAL(valueChanged(int)),
spinBox, SLOT(setValue(int)));
connect(slider, SIGNAL(valueChanged(int)),
this, SLOT(updateStatusBarIndicator(int)));

, - ,
.

:
connect(lcd, SIGNAL(overflow()),
this, SLOT(handleMathError()));
connect(calculator, SIGNAL(divisionByZero()),
this, SLOT(handleMathError()));

- -- -.

:
connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SIGNAL(updateRecord(const QString &)));

, .
, -
-.

:
disconnect(lcd, SIGNAL(overflow()),
this, SLOT(handleMathError()));

, Qt
, .
(..
):
connect(ftp, SIGNAL(rawCommandReply(int, const QString &)),
this, SLOT(processReply(int, const QString &)));

, , .
"" .
, , Qt
. Qt ,
( connect()).
Qt
Qt -- C++
,
, .
Meta Object System
: - .
QObject ,
, .
( Qt Designer) (
).
C++ ,
Qt.
moc ( ).
Q_OBJECT
C++,
C++.
:

Q_OBJECT ,
QObject: metaObject(), className(), tr() .
moc ,
Q_OBJECT.
connect() disconnect().

, qmake moc,
. --

Qt
, moc, -- .
. ,
QObject
. ,
QObject:
class Employee : public QObject
{
Q_OBJECT
public:
Employee() { mySalary = 0; }
int salary() const { return mySalary; }
public slots:
void setSalary(int newSalary);
signals:
void salaryChanged(int newSalary);
private:
int mySalary;
};
void Employee::setSalary(int newSalary)
{
if (newSalary != mySalary) {
mySalary = newSalary;
emit salaryChanged(mySalary);
}
}

setSalary(). salaryChanged()
, newSalary != mySalary.

.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

2.3. .
Qt ,
. , Qt Designer
, .
, Qt Designer, Go-to-Cell ("
"), 2.4. , - , Qt Designer,
:

.
.
Tab.
-
, .

2.4. .

Qt Designer, Qt 3.2.x|Qt Designer "" --


Windows designer -- Unix. ,
. "Dialog"
OK.
"Form1".

2.5. Qt Designer .

. , ,
TextLabel, --
"".
(LineEdit), (Spacer) (PushButton).
, 2.6.
.
, .
(spacer) .
.

2.6. .

, ,
Qt Designer.
1. TextLabel name "label",
text -- "&Cell Location:".
2. LineEdit name "lineEdit".
3. orientation "Horizontal".
4. name "okButton",
enabled -- "False", default -- "True" text -- "OK".
5. . name "cancelButton",
text -- "OK".
6. name
"GoToCellDialog", caption -- "Go to Cell".
, ,
Alt+C.
"&Cell Location:". Tools|Set Buddy (
). , ,
, LineEdit.
. --
, . ,
, buddy .

2.7. .

-- :

1. . Shift --
. .
Layout|Lay Out Horizontally.
2. . Shift --
"OK", "Cancel".
Layout|Lay Out Horizontally.
3.
Layout|Lay Out Vertically.
4. Layout|Adjust Size.
, ,
.

2.8. .

Tools|Tab Order. ,
, .
Tab Esc.

2.9. Tab.

.
-- .
Edit|Connections, :

2.10. .

2.11. .

: Edit Slots....
(. . 2.11).
enableOkButton().

. --
"New" Sender, Signal, Receiver Slot,
. :
okButton
cancelButton
lineEdit

clicked()
clicked()
textChanged(const QString &)

GoToCellDialog
GoToCellDialog
GoToCellDialog

accept()
reject()
enableOkButton()

, - Preview|Preview Form.
Tab. Alt+C ( ).
Cancel, .
gotocelldialog.ui gotocell
main.cpp, , :
#include <qapplication.h>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GoToCellDialog *dialog = new GoToCellDialog;
app.setMainWidget(dialog);
dialog->show();
return app.exec();
}

Makefile qmake (qmake -project; qmake gotocell.pro).


qmake gotocelldialog.ui Makefile
gotocelldialog.h gotocelldialog.cpp. .ui
C++ uic (User Interface Compiler - ).
Qt Designer- ,
C++.
() .
Qt Designer .
, :

"OK" .
,
, .

.
,
. :
#include <qvalidator.h>
void GoToCellDialog::init()
{
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp, this));

}
void GoToCellDialog::enableOkButton()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}

init() (
uic). LineEdit.
Qt , : QIntValidator,
QDoubleValidator QRegExpValidator.
, : "[A-Zaz][1-9][0-9]{0,2}". : "
, ,
1 9, , 0 9".
this ( QRegExpValidator()),
QRegExpValidator , GoToCellDialog.

.
enableOkButton() "OK", ,
.
QLineEdit::hasAcceptableInput(),
QRegExpValidator, init().

2.12. .

. Qt Designer --
gotocelldialog.ui, gotocelldialog.ui.h. .

"A12" -- "OK" .
,
. "Cancel", .
Qt Designer ,
Qt Designer-.
gotocelldialog.ui ( XML), - gotocelldialog.ui.h. , gotocelldialog.ui.h
.
Qt Designer (
), , ,
. ,
Go-to-Cell GoToCellDialogImpl,
GoToCellDialog .
, :
#ifndef GOTOCELLDIALOGIMPL_H
#define GOTOCELLDIALOGIMPL_H
#include "gotocelldialog.h"
class GoToCellDialogImpl : public GoToCellDialog
{
Q_OBJECT
public:
GoToCellDialogImpl(QWidget *parent = 0, const char *name = 0);
private slots:
void enableOkButton();
};
#endif

:
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qvalidator.h>
#include "gotocelldialogimpl.h"
GoToCellDialogImpl::GoToCellDialogImpl(QWidget *parent,
const char *name)
: GoToCellDialog(parent, name)
{
QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
lineEdit->setValidator(new QRegExpValidator(regExp, this));
}
void GoToCellDialogImpl::enableOkButton()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}

, ,
: GoToCellDialogBase, : GoToCellDialog.
- uic
. , , uic, subdecl, , -subimpl --
.
.ui.h,
, , uic,

. ,
"Designer Approach" ,
Qt Designer. , "Creating Dialogs",
, "Members"
(-) .