Qt学习之对话框与主窗口的创建
Qt中的信号与槽机制
Qt的元对象系统机制
用C++代码实现简单对话框
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QLabel>
#include <QDialog> class QCheckBox;
//class QLabel;
class QLineEdit;
class QPushButton; class FindDialog : public QDialog
{
Q_OBJECT
public:
//construct function
FindDialog(QWidget *parent = 0); signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
void findCliked();
void enabledFindButton(const QString &text); private:
QLabel *label;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
QLineEdit *lineEdit;
};
实现文件findDialog.cpp
#include <QtGui>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox> #include "finddialog.h" FindDialog::FindDialog(QWidget *parent)
:QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit); caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backword")); findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false); closeButton = new QPushButton(tr("&Clase"));
//链接信号与槽
connect(lineEdit, SIGNAL(textChanged(const QString)),
this, SLOT(enabledFindButton(const QString&))); connect(findButton, SIGNAL(clicked()),
this, SLOT(findCliked()));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(close())); //set layout,水平布局管理器
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);
//Adds a stretchable space (a QSpacerItem) with zero minimum size
//and stretch factor stretch to the end of this box layout.
rightLayout->addStretch(); QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
//set the dialog's main layout
setLayout(mainLayout); //设置主对话框的布局 setWindowTitle("Find"); //设置对话框的标题
setFixedHeight(sizeHint().height());
}
//实现槽
void FindDialog::findCliked()
{
QMessageBox msgBox;
msgBox.setText(tr("find successfully!"));
msgBox.exec();
QString text = lineEdit->text();
Qt::CaseSensitivity cs =
caseCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive; if(backwardCheckBox->isChecked()) {
emit findPrevious(text, cs); }else {
emit findNext(text, cs);
}
} void FindDialog::enabledFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
创建主窗口
主窗口是构建应用程序用户界面的框架,其中包括菜单,工具栏以及应用程序所需的对话框。那么该如何实现这些功能呢?
子类化QMainWindow(继承MainWindow)
class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(); protected:
void closeEvent(QCloseEvent *event); private slots:
void newFile();
void open();
bool save();
bool saveAs();
void find();
void goToCell();
void sort();
void about();
void openRecentFile();
void updateStatusBar();
void spreadsheetModified();
......
};
实现代码
MainWindow::MainWindow()
{
spreadsheet = new Spreadsheet;
setCentralWidget(spreadsheet); createActions(); //创建各个动作
createMenus(); //创建菜单
createContextMenu(); //创建内容菜单
createToolBars(); //创建工具栏
createStatusBar(); //创建状态栏 readSettings(); //读取配置 findDialog = 0; setWindowIcon(QIcon(":/images/icon.png"));
setCurrentFile("");
} void MainWindow::closeEvent(QCloseEvent *event)
{
if (okToContinue()) {
writeSettings();
event->accept();
} else {
event->ignore();
}
}
......
图形用户界面(GUI)应用程序通常会使用图片,在Qt使用图片的方法
<RCC>
<qresource>
<file>images/icon.png</file>
<file>images/new.png</file>
<file>images/open.png</file>
<file>images/save.png</file>
<file>images/cut.png</file>
<file>images/copy.png</file>
<file>images/paste.png</file>
<file>images/find.png</file>
<file>images/gotocell.png</file>
</qresource>
</RCC>
创建菜单和工具栏
Qt通过"动作"的概念简化了有关菜单和工具栏的编程。一个Action可以添加到任意数量的菜单和工具栏上的像。实现步骤:
void MainWindow::createActions()
{
newAction = new QAction(tr("&New"), this); //实现创建新文件的动作
newAction->setIcon(QIcon(":/images/new.png"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new spreadsheet file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile())); openAction = new QAction(tr("&Open..."), this); //实现打开文件的动作
openAction->setIcon(QIcon(":/images/open.png"));
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open an existing spreadsheet file"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
.........
创建菜单
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
fileMenu->addAction(recentFileActions[i]);
fileMenu->addSeparator();
fileMenu->addAction(exitAction); editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(deleteAction);
.............
设置上下文菜单和工具栏
void MainWindow::createContextMenu()
{
spreadsheet->addAction(cutAction);
spreadsheet->addAction(copyAction);
spreadsheet->addAction(pasteAction);
spreadsheet->setContextMenuPolicy(Qt::ActionsContextMenu);
} void MainWindow::createToolBars()
{
fileToolBar = addToolBar(tr("&File"));
fileToolBar->addAction(newAction);
fileToolBar->addAction(openAction);
fileToolBar->addAction(saveAction); editToolBar = addToolBar(tr("&Edit"));
editToolBar->addAction(cutAction);
editToolBar->addAction(copyAction);
editToolBar->addAction(pasteAction);
editToolBar->addSeparator();
editToolBar->addAction(findAction);
editToolBar->addAction(goToCellAction);
}
设置状态栏
void MainWindow::createStatusBar()
{
locationLabel = new QLabel(" W999 ");
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint()); formulaLabel = new QLabel;
formulaLabel->setIndent(3); statusBar()->addWidget(locationLabel);
statusBar()->addWidget(formulaLabel, 1); connect(spreadsheet, SIGNAL(currentCellChanged(int, int, int, int)),
this, SLOT(updateStatusBar()));
connect(spreadsheet, SIGNAL(modified()),
this, SLOT(spreadsheetModified())); updateStatusBar();
}
附上以上实现的简单图形界面程序(Spreadsheet)
Qt学习之对话框与主窗口的创建的更多相关文章
- qt学习(四)主窗选钮,显示新窗口。
游戏有选区这个习惯, 当然,我特指<冒险岛>了,有的时候就是打开一个主屏幕上五个按钮让你点击进入, 甚至有的时候进去了还要选哪个频道,游戏服务器都得分区,频道来完成功能.现在我们先进入想选 ...
- QT学习 之 对话框 (四) 字体对话框、消息对话框、文件对话框、进程对话框(超详细中文注释)
QMessageBox类: 含有Question消息框.Information消息框.Warning消息框和Critical消息框等 通常有两种方式可以来创建标准消息对话框: 一种是采用“基于属性”的 ...
- QT学习(对话框)codeblock版本
参考: http://www.cnblogs.com/JohnShao/archive/2011/09/26/2191627.html http://www.cnblogs.com/xiao-chen ...
- MFC/QT 学习笔记(三)——MFC模板创建
新建项目->MFC模板->MFC应用程序->应用程序类型:单个文档:项目样式:MFC 标准->下一步...OK 此时点击运行,可直接弹出窗口. 调整 视图->类视图: · ...
- Qt 学习之路 2(14):对话框数据传递
Home / Qt 学习之路 2 / Qt 学习之路 2(14):对话框数据传递 Qt 学习之路 2(14):对话框数据传递 豆子 2012年9月15日 Qt 学习之路 2 53条评论 对话框 ...
- QT学习之路--创建一个对话框
Q_OBJECT:这是一个宏,凡是定义信号槽的类都必须声明这个宏. 函数tr()全名是QObject::tr(),被他处理过的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用. 对于QT学 ...
- qt学习笔记
Part 1基本思路 学习目标:(熟练)使用 qt 制作符合要求的 ui FAQ:1)学习Qt5还是Qt4? 重要的是学习的方法而不是内容,掌握了正确的学习方法就可以很快完成另一者的学习,综合来看,Q ...
- QT学习2
一.常用控件与常用的功能函数. QDialog.QMainWindow.QPushButton.QLabel.QLineEdit 构造函数指定父容器.setText,getText,size,res ...
- PyQt(Python+Qt)学习随笔:Qt Designer中的menu菜单及menu bar菜单栏
菜单由menu bar菜单栏和menu菜单两部分构成,分别对应类QMenuBar和QMenu. menuBar是包含一系列下拉菜单项组成,menu包含两种,一种是直接对应Action的,一种是父菜单, ...
随机推荐
- JS日期、时间 格式化转换方法
Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...
- 谷歌pagerank算法简介
在这篇博客中我们讨论一下谷歌pagerank算法.这是参考的原博客连接:http://blog.jobbole.com/71431/ PageRank的Page可是认为是网页,表示网页排名,也可以认为 ...
- Java 中类的初始化过程
先来一张 JVM 中的内存模型 . 在Java 虚拟机原理这本书中介绍了类会被初始化的 5 种情况 . 1 遇到 new getstatic putstatic 和 invokestatic 这 4 ...
- C#中的特性 (Attribute) 入门 (二)
C#中的特性 (Attribute) 入门 (二) 接下来我们要自己定义我们自己的特性,通过我们自己定义的特性来描述我们的代码. 自定义特性 所有的自定义特性都应该继承或者间接的继承自Attribut ...
- SPOJ8791 DYNALCA LCT
考虑\(LCT\) 不难发现,我们不需要换根... 对于操作\(1\),\(splay(u)\)然后连虚边即可 对于操作\(3\),我们可以先\(access(u)\),然后再\(access(v)\ ...
- bzoj 2406 二分+有源有汇上下界网络流可行流判定
弱爆了,典型的行列建模方式,居然想不到,题做少了,总结少了...... 二分答案mid s----------------------->i行-----------------------> ...
- GIT(4)----免输入账号和密码方法
1.全局方式 包删除了,再次下载也不需要在配置输入密码和用户名 参考资料:git pull 和 git push免输入账号和密码方法 2.局部方式 包删除了,密码用户也会被删除,需要重新设置 打开终端 ...
- HDU 4649 Professor Tian(反状态压缩dp,概率)
本文出自 http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一 ...
- Tasker to proximity screen on
in my previous entry, i posed an idea how to use the built-in proximity sensor to turn the screen of ...
- Android 手机 ADB FastBoot 命令基本用法
adb用法: 准备: 1.在电脑上安装相应的USB驱动,在各分区置顶帖子有下载链接 2.手机进入设置->开发人员选项->勾选USB调试 adb devices 查看是否有设备 adb sh ...