第一部分Qt基础课件_第1页
第一部分Qt基础课件_第2页
第一部分Qt基础课件_第3页
第一部分Qt基础课件_第4页
第一部分Qt基础课件_第5页
已阅读5页,还剩78页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter2QT部件与事件处理课程主要内容用户界面组件介绍部件的布局管理通用部件部件的尺寸策略QtDesigner顶层窗体Qt图标Qt事件处理用户界面组件介绍用户界面组件用户界面由单个部件(widget)构成QLabelQPushButtonQLineEditQDoubleSpinBoxQScrollBar部件中的部件部件被分层次放置容器类提供可视化结构,但同时也是具有一定功能化的。

QRadioButton,需要用彼此间实现互斥,可以将多个QRadioButton放到一个GroupBox中。QGroupBoxQTabWidget部件的特点占据屏幕中一个方形的区域从输入设备接收事件当部件产生变化时,发出信号多个部件以层次式的方法组合构建一个部件中可以包含其他部件部件的布局管理一个对话框例子部件被放置在布局管理器中—使用户界面具有弹性易伸缩弹性好在哪里?让部件的大小适应内容让部件适应翻译变化让部件适应用户设置,如字体设置等布局管理QGridLayoutQFormLayoutQVBoxLayoutQHBoxLayout几种可用的布局布局管理器和部件“协商”各个部件大小与位置弹簧可以用来填充空白处一个对话框例子对话框由多个层次的布局管理器和部件组成对话框例子

QVBoxLayout*outerLayout=newQVBoxLayout(this);

QHBoxLayout*topLayout=newQHBoxLayout();

topLayout->addWidget(new

QLabel("Printer:"));

topLayout->addWidget(c=newQComboBox());

outerLayout->addLayout(topLayout);

QHBoxLayout*groupLayout=newQHBoxLayout();...

outerLayout->addLayout(groupLayout);

outerLayout->addSpacerItem(new

QSpacerItem(...));

QHBoxLayout*buttonLayout=newQHBoxLayout();

buttonLayout->addSpacerItem(new

QSpacerItem(...));

buttonLayout->addWidget(new

QPushButton("Print"));

buttonLayout->addWidget(new

QPushButton("Cancel"));

outerLayout->addLayout(buttonLayout);对话框例子QHBoxLayout*groupLayout=newQHBoxLayout();

QGroupBox*orientationGroup=newQGroupBox();QVBoxLayout*orientationLayout=newQVBoxLayout(orientationGroup);orientationLayout->addWidget(new

QRadioButton("Landscape"));orientationLayout->addWidget(new

QRadioButton("Portrait"));groupLayout->addWidget(orientationGroup);QGroupBox*colorGroup=newQGroupBox();QVBoxLayout*colorLayout=newQVBoxLayout(colorGroup);colorLayout->addWidget(new

QRadioButton("BlackandWhite"));colorLayout->addWidget(new

QRadioButton("Color"));groupLayout->addWidget(colorGroup);Horizontalbox,包含

groupboxes,verticalboxes,radiobuttons对话框例子可以使用Qt设计器来建立同样的结构通用部件通用部件Qt包含针对所有常见需求的大量通用部件Qt设计器中为部件组提供很好的概貌通用部件―按钮所有按钮继承自

QAbstractButton这个基本类。信号clicked()–当按钮被按下(并弹起后)发出。toggled(bool)–当按钮的状态发生改变时发出。属性checkable–当按钮可检查时为真。使按钮激活。checked–当按钮被标记时为真。(用于复选或单选按钮)text–按钮的文本。icon–按钮的图标(可以和文本同时显示)。QAbstractButtonQPushButtonQCheckBoxQRadioButton通用部件―列表项部件QListWidget用于显示列表项添加项目addItem(QString)–将项目附加到列表末端insertItem(introw,QString)–将项目插入到指定行选择项目selectedItems–返回QListWidgetItem的列表,使用

QListWidgetItem::text来形成文本信号itemSelectionChanged–当选择状态改变时发出QComboBox

以更紧密的格式展示一个单选的项目列表。QListWidgetQComboBox通用部件―容器容器部件用来结构化用户界面一个简单的

QWidget

对象可当做容器来使用设计器:将部件放置在容器中并为容器提供一个布局管理器代码:为容器创建一个布局管理器并将部件添加进布局管理器(布局管理器以容器为父对象)QGroupBox*box=newQGroupBox();QVBoxLayout*layout=newQVBoxLayout(box);layout->addWidget(...);...QGroupBoxQTabWidgetQFrame通用部件―输入部件使用QLineEdit

实现单行文本输入信号textChanged(QString)–文本状态改变时发出editingFinished()–部件失去焦点时发出returnPressed()–回车键被按下时发出属性text–部件的文本maxLength–限定输入的最大长度readOnly–设置为真时文本不可编辑(仍允许复制)QLineEdit通用部件―输入部件使用QTextEdit

QPlainTextEdit

实现多行文本输入SignalstextChanged()-文本状态改变时发出属性plainText–无定义格式文本html–HTML格式文本readOnly–设置为真时文本不可编辑QComboBox

通过editable属性使其可编辑SignalseditTextChanged(QString)–当文本正被编辑时发出属性currentText–combobox的当前文本QComboBoxQTextEdit通用部件―输入部件编辑整型数据有许多可选的输入部件也有许多用于double,time和date类型的部件信号valueChanged(int)–当数值更新时发出属性value–当前值maximum–最大值minimum–最小值QSliderQScrollBarQDialQSpinBoxQAbstractSlider通用部件―显示部件QLabel

部件显示文本或者图片属性text–标签文本pixmap–显示的图片QLCDNumber

用于显示整形数值属性intValue–显示的数值(使用display(int)函数进行设置)QLabelQLCDNumberQLabel通用部件―属性所有部件有一系列继承自QWidget类的共同属性enabled–用户交互可用或不可用visible–显示或不显示(show或hide函数)这些属性同时影响到子部件

例如使一个容器部件不可用时:QMessageBox信息框是可以显示提示信息,并接受用户按钮输入的一种对话框信息框使用方式一:静态函数StandardButton

QMessageBox::warning(QWidget*parent,constQString&title,constQString&text,StandardButtonsbuttons=Ok,StandardButton

defaultButton=NoButton)Parent:父组件指针Title:标题Text:提示文本Buttons:提示框中的按钮,可用或(|)运算添加多个按钮defaultButton:默认选中的按钮类似函数还有QMessageBox::information(…),QMessageBox::critical(…),QMessageBox::question(…),QMessageBox::about(…),…QMessageBoxintret=QMessageBox::warning(this,tr("MyApplication"),tr("Thedocumenthasbeenmodified.\n""Doyouwanttosaveyourchanges?"),QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save);switch(ret){caseQMessageBox::Save://Savewasclickedbreak;caseQMessageBox::Discard://Don'tSavewasclickedbreak;caseQMessageBox::Cancel://Cancelwasclickedbreak;default://shouldneverbereachedbreak;}QMessageBox信息框使用方式二:构造函数QMessageBox::QMessageBox(Iconicon,constQString&title,constQString&text,StandardButtonsbuttons=NoButton,QWidget*parent=0,Qt::WindowFlagsf=Qt::Dialog|Qt::MSWindowsFixedSizeDialogHint)icon:图标,可取值为MessageBox::NoIcon,QMessageBox::Question,QMessageBox::Information,QMessageBox::Warning,QMessageBox::CriticalTitle:标题Text:提示文本Buttons:提示框中的按钮,可用或(|)运算添加多个按钮parent:父组件指针F:窗口系统属性QMessageBoxQMessageBox

message(QMessageBox::NoIcon,tr("MyApplication"),tr("Thedocumenthasbeenmodified.\n""Doyouwanttosaveyourchanges?"),QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);switch(message.exec()){caseQMessageBox::Save://Savewasclickedbreak;caseQMessageBox::Discard://Don'tSavewasclickedbreak;caseQMessageBox::Cancel://Cancelwasclickedbreak;default://shouldneverbereachedbreak;}部件的尺寸策略尺寸(size)的策略布局是在布局管理器和部件间进行协调的过程布局管理器提供布局结构水平布局和垂直布局网格布局部件则提供各个方向上的尺寸策略最大和最小尺寸尺寸的策略printerList->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed)尺寸的策略每一个部件都有一个尺寸大小的示意(hint),给出水平和垂直方向上的尺寸的策略Fixed–规定了widget的尺寸,固定大小(最严格)Minimum–规定了可能的最小值,可增长Maximum

–规定可能的最大值,可缩小Preferred–给出最佳值,但不是必须的,可增长可缩小Expanding

–同preferred,但希望增长MinimumExpanding–同minimum,但希望增长Ignored

–忽略规定尺寸,widget得到尽量大的空间如果?2个

preferred相邻1个

preferred,1个

expanding2个

expanding相邻空间不足以放置widget(fixed)关于尺寸的更多内容可用最大和最小属性更好地控制所有部件的大小maximumSize–最大可能尺寸minimumSize–最小可能尺寸ui->pushButton->setMinimumSize(100,150);ui->pushButton->setMaximumHeight(250);子类化QDialogFind对话框Finddialog.h

#ifndefFINDDIALOG_H#defineFINDDIALOG_H#include<QDialog>classQCheckBox;classQLabel;classQLineEdit;classQPushButton;classFindDialog:publicQDialog

{ Q_OBJECT public:

FindDialog(QWidget*parent=0); signals: voidfindNext(const

QString&str,Qt::CaseSensitivity

cs);voidfindPrevious(const

QString&str,Qt::CaseSensitivity

cs);

privateslots: voidfindClicked(); voidenableFindButton(const

QString&text); private:

QLabel*label;

QLineEdit*lineEdit;

QCheckBox*caseCheckBox;

QCheckBox*backwardCheckBox;

QPushButton*findButton;

QPushButton*closeButton;};#endif

Finddialog.cpp

#include<QtGui>#include"finddialog.h"

FindDialog::FindDialog(QWidget*parent):QDialog(parent){ label=newQLabel(tr("Find&what:"));

lineEdit=newQLineEdit; label->setBuddy(lineEdit);

caseCheckBox=newQCheckBox(tr("Match&case"));

backwardCheckBox=newQCheckBox(tr("Search&backward"));

findButton=newQPushButton(tr("&Find"));

findButton->setDefault(true);

findButton->setEnabled(false);

closeButton=newQPushButton(tr("Close"));

connect(lineEdit,SIGNAL(textChanged(const

QString&)),this,SLOT(enableFindButton(const

QString&)));

connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));

connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

QHBoxLayout*topLeftLayout=newQHBoxLayout;

topLeftLayout->addWidget(label);

topLeftLayout->addWidget(lineEdit);

QVBoxLayout*leftLayout=newQVBoxLayout;

leftLayout->addLayout(topLeftLayout);

leftLayout->addWidget(caseCheckBox);

leftLayout->addWidget(backwardCheckBox);

QVBoxLayout*rightLayout=newQVBoxLayout;

rightLayout->addWidget(findButton);

rightLayout->addWidget(closeButton);

rightLayout->addStretch();

QHBoxLayout*mainLayout=newQHBoxLayout;

mainLayout->addLayout(leftLayout);

mainLayout->addLayout(rightLayout);

setLayout(mainLayout);

setWindowTitle(tr("Find"));

setFixedHeight(sizeHint().height());}Find对话框的窗体布局布局管理器的父子层次关系find对话框中所用到的槽

voidFindDialog::findClicked(){

QStringtext=lineEdit->text();

Qt::CaseSensitivity

cs=

caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;if(backwardCheckBox->isChecked()){emitfindPrevious(text,cs);}else{emitfindNext(text,cs);}}

voidFindDialog::enableFindButton(const

QString&text){

findButton->setEnabled(!text.isEmpty());}创建main.cpp来测试这些文件

#include<QApplication>#include"finddialog.h"

int

main(int

argc,char*argv[]){

QApplication

app(argc,argv);

FindDialog*dialog=newFindDialog;dialog->show();returnapp.exec();}QtDesignerQtDesignerQt应用程序除了使用手工编写代码的方式外,还可以用过QtDesinger来完成QtDesigner曾是一个独立的Qt桌面工具,现在集成于QtCreator中只需要拖动相应的控件输出为.ui文件,内容其实就是XMLUic编译器把.ui

文件转换成.h文件myproject.ui->ui_myproject.h设计器介绍sources*.cppexecutablesobjectfiles*.oheaders*.hgeneratedmoc_*.cppuserinterfaces*.uiincludescompileslinkscompilesmocs设计器介绍sources*.cppexecutablesobjectfiles*.oheaders*.hgeneratedmoc_*.cppgeneratedui_*.huserinterfaces*.uiincludescompileslinkscompilesmocsuic使用代码#ifndefWIDGET_H#defineWIDGET_H#include<QWidget>namespaceUi{classWidget;}classWidget:publicQWidget{Q_OBJECTpublic:

Widget(QWidget*parent=0);~Widget();private:

Ui::Widget*ui;};#endif//WIDGET_HUi::Widget类的前置声明一个

Ui::Widget

类指针ui

,指向所有部件基本上是一个标准的

QWidget

派生类使用代码#include"widget.h"#include"ui_widget.h"Widget::Widget(QWidget*parent):

QWidget(parent),

ui(new

Ui::Widget){

ui->setupUi(this);}Widget::~Widget(){deleteui;}实例化类Ui::Widget

ui删除

ui对象调用函数

setupUi,生成所有父窗体

(this)的子窗体部件使用设计器基本工作流程粗略地放置部件在窗体上从里到外进行布局,添加必要的弹簧进行信号连接在代码中使用在整个过程中不断修改编辑属性实践创造完美!使用设计器在代码中使用通过ui类成员使用访问其所有子部件classWidget:publicQWidget{...private:

Ui::Widget*ui;};voidWidget::memberFunction(){

ui->pushButton->setText(...);}54快速设计对话框Qt设计师的界面直接拖放所需要的部件为窗体设置一些属性带布局的窗体设置窗体的Tab建顺序保存和使用窗体把对话框保存到gotocell目录下,另存为gotocelldialog.ui,然后使用一个文本编辑器在同意目录下创建一个main.cpp接下来运行qmake生成pro文件,qmake会自动加入ui文件在工程中Qmake非常智能,它会自动生成适当的makefile规则来调用Qt用户界面编译器(uic)Uic工具会将ui文件转化为*.h文件

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论