qt (2) —— 部分部件

本文深入探讨Qt开发,涵盖Qt部件命名规则、保护槽、元对象系统和信号槽机制。通过实例介绍QSlider、QProgressBar、QLineEdit、QListWidget、QPushButton、QLabel、QComboBox、QSpinBox、QCalendar及布局管理,帮助理解Qt部件功能和交互设计。
摘要由CSDN通过智能技术生成

本文大部分内容学习自《Foundations of Qt Development》

notes

qt name rules

类的名字以大写字母开始,每一个新的词也以大写字母开头,也即骆驼拼写法 (CamelCasing)
方法的名字也遵从CamelCase,但是需要注意的是它以小写字母开头。

protected slots

被protect的slot只有被用作方法的时候才会被保护。私有的或者受保护的slots仍然能够用于connect sigals

meta-object和信号槽机制

meta-object自己知道signals和slots,meta-object compiler (moc) 会用c++实现相关的内容。

signals and slots

qt使用指针实现信号槽,当我们发出信号,实际上也就在调用信号方法,该方法被moc实现。signal方法随即调用slots。源对象和目标对象就是指向QObject或者它的子类的指针。

LIFO and FIFO

LIFO means “last in, first out”. 代表:栈
FIFO means “fist in, first out”. 代表:队列

Qt designer

命令行启动:shell> designer
qt4可能是shell> designer-qt4

widgets

QSlider and progressBar

下面的小应用由dialog, horizontalSlider, progressBar, pushButton, label构成。
第三个progressBar的范围是(0,0),结果时不显示数值结果并不断运动的,第4个progressBar的范围我想设置成无穷大,但不确定这种方法setRange(-1,-1)是否正确。

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
/*
 *for QProgressBar format : QString
 *This property holds the string used to generate the current text.
 *%p - is replaced by the percentage completed. %v - is replaced by the current value. %m - is replaced
 *by the total number of steps. The default value is "%p%".
 */
    ui->progressBar->setFormat("%p%");  // default
    ui->progressBar_2->setFormat("%v out of %m"); //custom text
    ui->progressBar_3->setRange(0,0);   // hide text and keep moving.
    ui->progressBar_4->setRange(-1,-1); // static
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->progressBar_2,SLOT(setValue(int)));
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->progressBar_3,SLOT(setValue(int)));
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->progressBar_4,SLOT(setValue(int)));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close()));
}

这里写图片描述

QLineEdit

LineEdit的echo mod可以设置成不同的格式,属性MaxLength则设定了能够填写的最大字符数。

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    ui->lineEdit_3->setMaxLength(9);
    connect(ui->lineEdit,SIGNAL(textChanged(QString)),ui->lineEdit_4,SLOT(setText(QString)));
    connect(ui->lineEdit_2,SIGNAL(textChanged(QString)),ui->lineEdit_5,SLOT(setText(QString))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值