Tải bản đầy đủ (.doc) (17 trang)

Tiểu luận: Tìm hiểu và sử dụng Qt để viết chương trình tạo một cửa sổ chương trình điển hình docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.01 MB, 17 trang )



Bài 14. Tìm hiểu và sử dụng Qt để viết chương trình tạo một cửa sổ chương
trình điển hình.

 !"#$$$"#"
% !"#$#"&'(
 !" #$%&'(
)*
+,-./01234567.5.-*89,2
301:;+5.<=5>?=@A+B56,-CD:DE
3FGH,IJ*K=59+7=5LCDM/NOCC4-P565Q
%CE7LRST@.M=56:UMVNWNX8X@H5<<Q56MY
35QN7QN9C.C>CZ=5[,-<QMY37
N9C.CC4%C\*K/].C4^=+=5_56Q
]=2N`a76*[=<Q+CD5Q%N9C.C
C4@%<MY=5U-7:D>`^=+[=bN/VN
C_>5_MY=7=2IE9N/CZ-:DC_>=N/C=VNWN
C47I=*
+7H=7,-CD)ST7V56,AN7-==Dc-!7Md
%CeC-!f-QM=*T,AN75=9QM=+*
+5_56I=C=,4cgC"##hALD5D+--,C4-
=g7V=*+a767VC47,AN7=@S==:=MH@
7=NMi
QCU:A^=+7H=7:=>C
• 7MMjN,=d7C@<<Q47C?5.-!=@X0@kGM*
• <H0jH75lN*
• ma76CH7FCH7,-CD==HH7-<Q,AN756IH@
-0-IH7V=J*
• !N`nP\oo-K[=IEN/7/:V2CDI/
N`-5QG,=-MY3<*


))*/.
,I-,IM=51Npp]jN7qH*7pG,=M-/.:/`6N5.
--7r*
)))*-5e
-5e+7H=77V-*[=b9?sH0t-L56I<4
0*
)*4+7H=7-4CD235/*
[=MuCQvI%2:/.+:;4CD23HN=5
/MY3oo-CEj5kH+*[=MY3+7H=7)ST-+
SHMH75Q47=vB,*
9[=MY3+7H=75Q47=CDN7qHd,H9%*=5<@MY
3+SHMH75QbMY=d,H=L5Q4CD23bMY=
g:/-CD[V7CDY=MU7C-@CD=E3@CDGH
I*<,-CD23+5/*\@[=CL
23:;47=-5DCZ-,d,H*
')*+,-+./0+/,12
+74CDN7qHC7+7H=756a76:ZCDGw=7F7AMxJ
:4c:*kw=7Muy=AN-5e9%c,4N7qH
3Q-47=N7qH[=*
Q4N7qHHN=@?f,HzHGf,H787qHz{NN,=Mz+{NN,=z
MH@-,-CH^=Gw=7*7DN4,=MM)d7C=@|,N,-
HN=-?+C=kG,-,NMZ*
kw=7+{NN,=47=CDN7qH<2=CDM7Hd,H-CDM\d,H05r
CD=LFHN=GHJ
HN=*N7 N7qHd,H*
C=*NNjd,H>`23*
HN=*NNjd,H>^=,NHN=^=HN=GH*
HN=*jd,HH=H7^=,NHN=HN=GH*
HN=*j=LHN=GH*
%)3+4-0/56/

kw=747=54C_M=-d,HC=*NN
#include "notepad.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Notepad w;
w.show();

return a.exec();
}
m=B59I=:d,HH=H7HN=GH-+{NN,=*
SB&5rx=-CC=Hoo23*
SB}47=CD5\6+{NN,=*\6-]/,~23-9%5Q
4:VI•7+-MY3+kHM*<IZ4CD5\623
=7=CM\47=7*F\23=C-IEMY3+
kH@=<QMY3+{NN,==%*J
SB€47=5\6HN=*1,-5\6C-Gw=747=,N-AN
=*=L2=%\r56?,-GHFA3J
7+*`3,-GHHH0@=D@_@-[7=*!DGHP
<Q2=GHI@CDDN4eCDY=MU23`•4*
SBhQrGHHN=7C-7CDY=MU7*GHP<
2g2=F`3CD+!=kG@75<<=E3@CH@=74
@-CD-GHIJ@<QQrCDGHV7CDY=MU7*
kH56Ce5r,-IEV‚-CMGFJ,-CGH7=*
SB$#[NICD23+5=4@MI5647=-Y5%
GH^=23*`3^=MI,-D-VN`C*
()787$!1+29  !:;2< !
kw=747=CD5rx==L75r4ƒ!X@HN=**R
[=CZd,HHN=*7+7H=7@<Mu5DCZ7+SHMH7*

R:,23@+7H=747:r=LF+„MH7)H7d=H
CN,H7jJ@5?d,H*-47=CDd,HH=H7oo2…HN=**
=)3>2? !@A/B! /-
kw=747=CD23MY3CD+!=kG*<<M:\7`7C-[=
<QCCD=CH@GHI@=E3@-CD=74*R
71C<Q,-:VI•,4GH-*
[=_MY3+SHMH75QCCD5\6+H0T-CD5\6
+8MK-Y=MU`*R[=|g:/7GHH0H@<A56
MIVN`C-N/2:;5Cg:/*[MuI†23
HN=IV*F<x=,-@VNDJ*
QCGH7+SHMH7
7%5D+7H=7T7@:VC5[N-d,HHN=*787qHM5QIZ5D
d,H7+SHMH7*
RW-/GHM=51-C-
• H0TF+H0TJ
• 8MKF+8MKJ
R`5[N-GH8MK-|+*
7Y=MUD`@=5U7r^=:qH=CH-]K*
V7,o{FeCo{J5Q?GH-:VC-X=H7=,,Fe
V7,oXJ5Q:\7`H.?F+K0X=J*
V7,oFe7,oJ5Q,=5U*
=L:1L7EM=7+SHMH7
[=<Q0HCd,Hƒ!X5647=77:rC_
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Notepad</class>
<widget class="QMainWindow" name="Notepad">
<property name="geometry">
<rect>
<x>0</x>

<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Notepad</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>70</x>
<y>0</y>
<width>268</width>
<height>235</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
<item>
<widget class="QPushButton" name="quitButton">
<property name="text">
<string>Quit</string>
</property>
</widget>
</item>
</layout>

</widget>
</widget>
<widget class="QMenuBar" name="menuBar">

")56/+/,12/12/-
kw=747=CDd,HH=H7,NHN=< ‡,H@ CDM77@ CD
HM77@-5\6„*f,HM=
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow
{
Q_OBJECT

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

private:
Ui::Notepad *ui;
};
#endif // NOTEPAD_H
SB&,H+!=kGVNCDY=MU23`*
SB}5rx=,NHN=7=CHMN=H„@5<,-=CHMN=HO,N„)
47=cd,H*:;,*
SB$#2=C=7+…ˆK‰T*<0V5975rx=,N@-:\
,N^==CD+ˆ:qH*@<PN/I%c=c+ˆ:qH*!D+ˆ:qH

<CCDM\I/gMCD,NooO*[~,-@,N-M,<
Q567V47jCH*P<Q7V,4=CM\^=CDM,-?<*
SB$Š5rx=CDM77<CD=CM\Ce5r56?,-N=7H*7r#
b7=7;GHIE<N=7HF<,-CDGHVN=VJ*
SB$}5rx=CDHM77/5Q/N<:D5_56MY3:Z5\
67B5L^=<*H]5e^=oo@-C^<,N
C-<56I=:@:y59:;CDV_F‹J*7+ˆ:qH@-C^/,-5Q5/C
:/7;-C^^=,N0V56?5[ICD5\6560<=E]=
CD7†5%,NMZ*
SB$'I=:CD:%-,-CD7†5%,NHN=„)*!D:%-
yCD,N3Q@-<Q%NAV/N2^=<*
C)+/,123+4-0/56/
7Hd,HC-Gw=747=,NHN=M=
#include "notepad.h"
#include "ui_notepad.h"
Notepad::Notepad(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Notepad)
{
ui->setupUi(this);
}
Notepad::~Notepad()
{
delete ui;
Œ
m=B59,Hd,HH=H7,NHN=5_5647=:ZGw=7-d,H
H=H7„)5_5647=:Z,*
SB&5rx=M77HN=-%,ANd,H„)*
SBŠ?M77+!=kG,-,NMZ,NHN=*
SB}47=CD5\6,N„)-<-*

SBh%,AN„)*
SB$$^*
D)-+./056/
kw=747=N7qHd,HHN=*N7[=M=
#
#
# Project created by QtCreator 2013-04-21T21:08:50
#
#
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Notepad
TEMPLATE = app
SOURCES += main.cpp\
notepad.cpp
HEADERS += notepad.h
FORMS += notepad.ui
87qHd,Hb5r23-C]C=IH5QMY3;C47=N7qH@P
M7Hd,H@H=H7d,H@-„)d,H2=7N7qH*
[=P<QMY3?]C=IH•M N7qH5Q4d,H*N7*!e@7
7L6N5<@N/CB+oŽGHM5Qd,H5647=,I%,4+kHM
!,H*
E)F$:G !0 !:;2< !
K1L[=<CD=L@<IEM,-C565.
v`@<b2=CDH0H-CD[V@PCDM\-CO5Q
@†-CZ7D23*Q,-C237Zv`@[=Mu:UM
CL-<*9@[=MuC2g-[V*
%NH@[=Mu:UMC-C5Q/CDd,H-H0H-5Q,D
^=H0H-CDd,H*
F$H I

m9%\5.IQC-<ONWNL
I†23*@7`3-[=MY3-C:/-5QC?=
,-C%-5Q<QCL-23*Q,-C5.-@[=
CCDM,I%\+:*
QI†23I[+56V@=MY3`+-%M,*
!D`56N7=ICDMI5e:0/7=-CDM,,-CD-C56?,-
5Q5N2CD`3Q*kH+5_05r7`-M,C-[=
<QMY37%Nc+SHMH7*
Y3+SHMH75QCCDM,-C@I`DN/-[+5QCZ
CD75v/-M=5<?M,z,IHFJ*
!DM,N7=H@…]K…,IHFJ@  56 C-d,HH=H7 ^=,NHN=
GH@HN=*-CD-CN7=H@HN=…]K…,IHFJ@56C-
M7Hd,H^=,NHN=GH@HN=*NN*[=b9%C_5Q-C
7M7Hd,H*
[=_-C_5_56MY=5U7H=H7d,H@HN=*
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QMainWindow>
namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow
{
Q_OBJECT

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

private slots:

void on_quitButton_clicked();
private:
Ui::Notepad *ui;
};
#endif // NOTEPAD_H
SB$&MY3`^=+-%M,5Q23I[+56V*
+SHMH7MY3+!H=ˆ:qH=jHd=,HM5QI%\5QI%\`
:•M,IHFJ5%CDM,7,NHN=*,5D47=C_7-C
HN„FJ^=DN45Q,-C5.-@A+SHMH7b9I=:@CD
M,CDO*
C_27M7Hd,H@HN=*NN@M=
void Notepad::on_quitButton_clicked()
{

Œ
!_05r-CN7=H56I+8MKN`,IHFJ*
K1L[=:UMH5QM,]FJ^=+{NN,=HN=
void Notepad::on_quitButton_clicked()
{
qApp->quit();
Œ
J)F$00/$$/ 4
EL@7CDY=MU`@CDM,56?:;.GH*`3
HCCH-[7=E3*Q,-C5.--@+VN
+{@<Q567=CDM\GH@-5_56I%\CDM,*`3@/
+!H-+,K=7<Q47=HCCH-[E3c+{*
QCQMY3<`-M,@[=MuCHCCH5QCZ
-,-,-I%\[-M,*
751@[=MY3+SHMH75QCGH-=L*
kw=747=CD23CD+!HGH@vNHmH7H5Qva

CH-HCCH*R`5[N-H05QANCHf,H@HCˆNH-HC
=H*+SHMH75D47=-5D`6N*
QI%\-5D-M,@I`DN/-CD-5D-?
M,z7H7HFJ*
7L6N^=+{5647=g:/Mu0V7GHC-[
=C[-F77L6N-,-HCCHJ*%[=C\C
-5D-CD=E3@[=<Q05r:Q6[*
!_MY=5U7HN=*:1LM=
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>

<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionOpen">
<property name="text">
<string>Open</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
</widget>
+SHMH7CN7=HM,…=ˆNH…7H7HFJ-…==H…7H7HFJ
-  HN=*  -    -C  N7=H  HN=…=ˆNH…7H7HFJ  -
HN=…==H…7H7HFJ-HN=*NN*
7N9M=@[=:UMH5Q/-,d,H*RCDHCCH56
VN@HCI`4-5D@-M,256?*
KLM6/
7N9-@[=2g^=M,…=ˆNH…7H7HFJ*K59
,-9LMY3^=AN5QCZ*+5I•C+d,HS=,@,-CD
DN4c5<L<Q?CDAN*0V^=DN4N3D-
./C`C-:4423*m/M=51VDN4 7
GGM
[=CH5647=:Z+SHMH77HN=*NN@M=
void Notepad::on_actionOpen_triggered()
{

QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
QString(),
tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, tr("Error"), tr("Could not
open file"));
return;
}
QTextStream in(&file);
ui->textEdit->setText(in.readAll());
file.close();
}
Œ
m-CM=HˆNHf,H=CHFJQrCDDN4N2d,H*<7/.5L
^=d,H56?@eCDa7a%L^:†DN4*
%[=<CDd,H@[=CZd,HNHFJ@7/.5[%d,H<QCZ
56*%d,HIEQCZ56@[=MY3+!HMM=HK05QQrCDDN
4CDE:,a*
5?v,MY3,N+H07H=C@C-2=:qH+f,H*m-C7H={,,FJ7/.D
^=d,HCD+7*DM=5<<Q56Qr7H0H*=5<
[=,MHFJd,H5Q7/,4d,H5.-*
K1L[=MY3-C7FJ*m-C-,-9%IC\%237.
Ev*
QMY3+f,HS=,@+f,H@+CHMM=HK0-+H07H=C@CBM=-
HN=*NN
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>

#include <QTextStream>
:4M6/
K1L@[=MuQM=M,…==H…7H7HFJ@MY3+f,HS=,5Q
47=CDDN475<L<Q,d,HV5r4r7`V5r*
[=CH5647=:Z+SHMH77HN=*NN@M=
void Notepad::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
QString(),
tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
// error message
} else {
QTextStream stream(&file);
stream << ui->textEdit->toPlainText();
stream.flush();
file.close();
}
}
Œ
R[=%D^=H0H-d,H@[=,4MY3,N+H07H=CCD
,9v=*+H07H=CP<Q%+7-d,HY••*
'&)F$1 0N !0?
=E356C-CDY=MU`=CH?{,
K=7cCHv/*-7=@%<CD=E3<7Y=MU`@[
=<QV-CP759:N/^=<5Q47=CD=E3C*
7=C:Q6-5DCZ-,d,H
R`5[ND7--5DZ{T7@Y=MUT=7=@ZC3)

[=?-5D*‘51[=?cd,H7HM7HM*
f,H7HM7HM*]75647=:Z7HN=^=GG:Z54HM=
•nSˆ’8T“z•“H7MŽ”$*#”z
•]7HM7Hz
•d,HzC=HMpNH*N•pd,Hz
•d,HzC=HMpM=H*N•pd,Hz
•p]7HM7Hz
•p“z
=,,4d,HHN=*]7-47=C3C=HM7>N9%-@Z
51,-NH-M=H*
%NH=CBM=-HN=*N75QCd,H7HM7HM-N7qH*
“Tˆ„“ToŽHN=*]7
-B,4,-?`6N=E3*
[^==E35647=-5D7{T7-IW-
=E3*S-5D<Q5654:ZCH-[7=
E3@[=<QQv=CH-=E3*
Q=M•CD-5Dv=CDCH-=E3@IW:Q6^=<c=
H7-=E3*
=E35647=*
'')F$O2!/2+08
=MuC-CDGH2=E.7*
=:y59:;CCDHC{:Z=CHˆNH-=H7>
C-5DHN=*
QI%\-5DHN=-M,@I`DN/-=HN=7{
T7-?M,z7H7HFJ*
[=CH5647=:Z+SHMH77HN=*NN@M=
void Notepad::on_actionNotepad_triggered()
{
QMessageBox::about(this, tr("About"),
tr("<b>Notepad 1.0.0</b>"));

Œ
'%)462P0*Q+/,12
K1L[=<V/AN9%@?K,zK,87qHHN=5Q:,
-423*+7H=7MY3]C=IH-C=IH5Q47=CD7
7C305r7%,AN:,^=-4<*
'()462P0*QR2S !69 
Q:,23cB,@QM=C3C-[=<d,H*NN^=23
-Cd,HN7qH*N756CE/751*,M=[N:,23
qmake
make (or nmake on Windows)
,47=CD77C3N7qH*E3]C=IH5?N7qH
d,H-47=CD!=IHd,H.:,23*E3C=IHFe
E3C=IHJM=5<5?!=IHd,H-47=7rN1*

×