Extension Example
/****************************finddialog.h****************************/
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
QT_BEGIN_NAMESPACE
//提前声明要用到的类
class QCheckBox;
class QDialogButtonBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QPushButton;
QT_END_NAMESPACE
//! [0]
class FindDialog : public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent = 0);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *fromStartCheckBox;
QCheckBox *wholeWordsCheckBox;
QCheckBox *searchSelectionCheckBox;
QCheckBox *backwardCheckBox;
QDialogButtonBox *buttonBox;
QPushButton *findButton;
QPushButton *moreButton;
QWidget *extension;
};
//! [0]
#endif
/*************************finddialog.cpp**********************************/
#include <QtGui>
#include "finddialog.h"
//! [0]
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
//当用户按下label指示的快捷键(shortcut key)后,
//键盘焦点转移到他的伙伴窗口lineEdit.
//Find &what:快捷键Alt+W。注意是&指定了快捷键
caseCheckBox = new QCheckBox(tr("Match &case"));
fromStartCheckBox = new QCheckBox(tr("Search from &start"));
fromStartCheckBox->setChecked(true);
//! [1]
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
//当用户按下回车键,当前默认按钮得到点击,
//而与当前对话框中那个窗口部件拥有键盘输入焦点无关。
//同一时刻只能有一个推动按钮被设置为默认按钮。
//这个按钮然后被显示有一个额外的框架(这依赖于图形用户界面风格)。
//http://www.kuqin.com/qtdocument/qpushbutton.html#default-prop
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
moreButton->setAutoDefault(false);
//当对话框接收到键盘输入焦点时,
//对话框中的自动默认按钮自动变为默认推动按钮。
//
buttonBox = new QDialogButtonBox(Qt::Vertical);//垂直分布
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
//! [1]
//! [2]
extension = new QWidget;
wholeWordsCheckBox = new QCheckBox(tr("&Whole words"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
searchSelectionCheckBox = new QCheckBox(tr("Search se&lection"));
//! [2]
//! [3]
//connect the More button's toggled() signal
//to the extension widget's setVisible() slot.
connect(moreButton, SIGNAL(toggled(bool)), extension, SLOT(setVisible(bool)));
//当按钮more被按下(toggled)拴住时,extension才显示被看见(Visible)
QVBoxLayout *extensionLayout = new QVBoxLayout;
extensionLayout->setMargin(0);//设置组件的边框和它的文本或子部件之间的空白(margin)。
extensionLayout->addWidget(wholeWordsCheckBox);
extensionLayout->addWidget(backwardCheckBox);
extensionLayout->addWidget(searchSelectionCheckBox);
extension->setLayout(extensionLayout);
//extension使用布局管理器extensionLayout
//! [3]
//! [4]
//topLeftLayout = label + lineEdit
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
//leftLayout = topLeftLayout + caseCheckBox + fromStartCheckBox
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(fromStartCheckBox);
leftLayout->addStretch(1);
//mainLayout = leftLayout + buttonBox + extension
QGridLayout *mainLayout = new QGridLayout;
mainLayout->setSizeConstraint(QLayout::SetFixedSize);//设置为固定大小(FixedSize)
mainLayout->addLayout(leftLayout, 0, 0);
mainLayout->addWidget(buttonBox, 0, 1);
mainLayout->addWidget(extension, 1, 0, 1, 2);//跨越一行两列
setLayout(mainLayout);
setWindowTitle(tr("Extension"));//设置标题
//! [4] //! [5]
extension->hide();//extension默认隐藏
}
//! [5]
/*****************************main.cpp***************************************/
#include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog dialog;
return dialog.exec();
}
/*************************************************************************/
分享到:
相关推荐
QT 例子QT 例子QT 例子QT 例子QT 例子
通过点击Qt中的pushbutton,可以弹出菜单,菜单选项是QAction,可以连接槽完成相应功能。Qt工程是基于Qt5.10.1 32位,Qt Creator 4.5.1(Community)
qt箭头按钮源码
QT串口通信例子,实现了QT用串口进行通信的例子
本资源利用Qt实现了按钮的状态变化,鼠标移动到按钮上按钮改变颜色,离开变回原来状态,点击按钮弹出一个对话框
Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt共享内存代码例子Qt...
Qt按钮菜单的代码示例。
本事例用Qt编写了一个ActiveX 使用的vs2010 Qt4.8.0
5. Qt工厂模式例子 6. 简单工厂模式 部分源码实例: // 奔驰工厂 class BenzFactory : public AFactory { public: ICar* CreateCar() { return new BenzCar(); } }; // 宝马工厂 class BmwFactory : public A...
qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子qt4 例子
经典Qt按钮互斥效果,按钮的三种状态,显示背景及前置背景,响应,利用纯代码实现。
qt 自动创建按钮
Qt 4.8.2 QPushButton 按钮贴图 源码 可执行文件
Qt个性按钮。 动态Led效果的按钮。
示例是单击qt界面上的一个按钮,然后程序自动关闭当前窗口,然后打开一个界面,类似于qq点击了qq登录按钮之后,界面有所变化
基于QT的高亮圆形按钮, 有抛光的效果。
5个qt多线程例子,关于qt 多线程 TCP UDP socket 仅供参考
1.数目可变的页码按钮 2.跳转到最开始及最后一页 3.跳转到指定页 4.控制每页显示的行数(或某控件数量) 5.提供示例可与分页查询相结合。 Qt [1] 是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发...
总结在一个自定义的按钮。该按钮可以任意摆放图片和文字位置和距离,同时可以用QSS设置按钮内图片、文字、整个按钮的样式表效果,也就是说,可以实现鼠标划过、鼠标选...编译环境为:Qt5.9.4+QtCreator4.5.0+MinGW32
我们都知道,QT自带的QPushButton类特效简单,尤其在做游戏的时候显得与项目格格不入,所以我们将QPushButton继承出来重写,通过enterEvent,重绘等多个函数进行美化