信号与槽
#include "helloqt.h"
#include "ui_helloqt.h"
helloqt::helloqt(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::helloqt)
{
ui->setupUi(this);
connect(ui->showchildbutton,&QPushButton::clicked,this,&helloqt::showchilddialog);//连接信号和你刚刚定义的槽——showchildbutton
//这里链接的是pushbutton按钮的点击信号和函数void helloqt::showchilddialog()
}
helloqt::~helloqt()
{
delete ui;
}
void helloqt::showchilddialog(){//本质是设置一个点击后运行的函数
QDialog* dialog=new QDialog(this);
dialog->setWindowTitle("子窗口");
QLabel* label=new QLabel(dialog);
label->setText("子窗口,你好");
dialog->show();
}
#ifndef HELLOQT_H
#define HELLOQT_H
#include <QMainWindow>
#include<QDialog>
QT_BEGIN_NAMESPACE
namespace Ui {
class helloqt;
}
QT_END_NAMESPACE
class helloqt : public QMainWindow
{
Q_OBJECT
public:
helloqt(QWidget *parent = nullptr);
~helloqt();
private:
Ui::helloqt *ui;
public slots:
void showchilddialog();//定义的槽函数
};
#endif // HELLOQT_H
运行效果为点击后生成子窗口:
通过设计器来编辑信号与槽:
鼠标左键加拖动:
左边选择信号是什么(这里是点击),右边是槽函数,这里是关闭。
.配置转到主页面按钮的时候,转到控件编辑模式,右键点击选择clicked
自动创建如下函数,自动关联
主函数如下:
#include "helloqt.h"
#include"mydialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog md;
//md.show();
if(md.exec()==QDialog::Accepted){//关于md.exec(),有了这个前面就不需要有md。show了
//判断你点击的到底是哪个按钮
helloqt w;
w.show();//显示主界面
return a.exec();//程序正常运行
}
else{
return 0;//退出程序
}
}
执行效果如下图:
这是新的重新登录的按钮的槽代码,效果和代码都如下:
void helloqt::on_pushButton_clicked()
{
close();//不再渲染窗口
MyDialog md;//再渲染一个登录窗口(可以是重新登录窗口)
if(md.exec()==QDialog::Accepted){//如果再次点击主页面了,就进入,反之直接结束了
show();//再次渲染窗口
}
}
标准对话框
先看看小部分有用的,其他的用到了再说。
关于输入对话框,先看看它的几个静态函数:
QString getText
{
QWidget *parent, //父窗口
const QString &title, //标题名
const QString &label, //标签提示
QLineEdit::EchoMode mode = QLineEdit::Normal, //QLineEdit控件的输入模式
const QString &text = QString(), //对话框弹出时QLineEdit控件中默认出现的文字
bool *ok = 0, //指示哪个按钮被触发
Qt::WindowFlags flags = 0 //指明对话框的窗体标识
};
int getInt
{
QWidget *parent,
const QString &title,
const QString &label,
int value = 0, //QSpinBox控件的默认显示值
int min = -2147483647, //指定QSpinBox控件的数值范围
int max = 2147483647,
int step = 1, //使用QSpinBox控件的步进值,也就是按上下一次增加多少
book *ok = 0,
Qt::WindowFlags flags = 0
};
QString getItem
{
QWidget *parent,
const QString &title,
const QString &label,
const QString &items, //指定QComboBox控件显示的可选条目是一个QStringList对象
int current = 0, //QComboBox控件中默认显示的条目的序号
bool *ok = 0,
Qt::WindowFlags flags = 0
};
double getDouble
{
QWidget *parent,
const QString &title.
const QString &label,
double valur = 0, //QSpinBox控件的默认显示值
double min = -2147483647, //QSpinBox控件的范围
double max = 2147483647,
int decimals = 1, //QSpinBox的步进值
bool *ok = 0,
Qt::WindowFlags flags = 0
};
代码实现如下:
#include "helloqt.h"
#include"mydialog.h"
#include <QApplication>
#include<QColorDialog>
#include<QDebug>
#include<QInputDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog md;
bool ok;
QString name= QInputDialog::getText(&md,QObject::tr("标准字符串输入对话框")
,QObject::tr("请输入姓名: "),QLineEdit::Normal,QObject::tr("你的名字是?"),&ok);
QStringList SexItems;
SexItems << QObject::tr("男") << QObject::tr("女")<<QObject::tr("武装直升机");
QString SexItem=QInputDialog::getItem(&md,QObject::tr("标准条目输入对话框")
,QObject::tr("请选择性别: "),SexItems,0,&ok);
int values=QInputDialog::getInt(&md,QObject::tr("标准int输入对话框")
,QObject::tr("请输入年龄: "),0,-200,200,1,&ok);
double d=QInputDialog::getDouble(&md,QObject::tr("标准double输入对话框")
,QObject::tr("请输入身高: "),0,-200,200,1,&ok);
//md.show();
if(md.exec()==QDialog::Accepted){//关于md.exec(),有了这个前面就不需要有md。show了
//判断你点击的到底是哪个按钮
helloqt w;
w.show();//显示主界面
return a.exec();//程序正常运行
}
else{
return 0;//退出程序
}
}
效果如图:
其他窗口部件
大部分怎么用还是查书吧,或者把名字搜一下看看用处。
QLabel中这个用来设置图片。
对于qstackedwodget的一个使用方法;
首先准备好一个页面栈和qlistwidget,然后编辑 信号与槽:
效果如图:
关于行编辑器QLineEdit:
显示模式有四种:
也即
输入123456后效果如下:
这里设置掩码(十个A表示输入十个字母)