Qt5开发之控件

来源:互联网 发布:linux 复位USB 编辑:程序博客网 时间:2024/06/06 00:54

一、LineEdit编辑框

//lineEdit中显示    ui->lineEdit->setEchoMode(QLineEdit::Normal);//输入原样显示    ui->lineEdit2->setEchoMode(QLineEdit::NoEcho);//输入不显示出来    ui->lineEdit3->setEchoMode(QLineEdit::Password);//输入密码样式显示    ui->lineEdit4->setEchoMode(QLineEdit::PasswordEchoOnEdit);//输入原样显示,离开后密码样式显示    //lineEidt中Placeholder显示文本    ui->lineEdit->setPlaceholderText("QLineEdit::Normal");    ui->lineEdit2->setPlaceholderText("QLineEdit::NoEcho");    ui->lineEdit3->setPlaceholderText("QLineEdit::Password");    ui->lineEdit4->setPlaceholderText("QLineEdit::PasswordEchoOnEdit");//lineedit右边显示图标,并点击删除lineedit中数据QAction *action = new QAction;    action->setIcon(QIcon("../icon/Sound.ico"));    ui->lineEdit_5->addAction(action,QLineEdit::TrailingPosition);    connect(action,SIGNAL(triggered(bool)),this,SLOT(onClear()));void lineEdie::onClear(){    ui->lineEdit_5->clear();}

echoMode

Normal:正常显示

Password:设置密码为小黑点显示

或者setEchoMode(QLineEdit::Password)

二、CheckBox多选按钮
多选框状态:
1、不选中:Qt::Unchecked
2、半选中(正方形实心):Qt::PartiallyChecked
3、全选中Qt::Checked


设置当前多选框状态:

ui->checkBox->setCheckState(Qt::PartiallyChecked);


判断当前状态:

ui->checkBox->checkState() == Qt::Unchecked


三、RadioBox单选按钮

判断当前状态:

if(ui->radiobox->isChecked())//选中

设置选中:

ui->radiobox->setChecked(true);//选中

ui->rediobox->setdisabled(false);//选中,和ch是置反


四、ComboBox下拉组合框

QStringList  items;

items.append("选项1");

items.append("选项2");

ui->comboBox->addItems(items);


获取当前选中项数据:

ui->comboBox->currentText();//String

获取当前选中项:

ui->comboBox->currentIndex();//int

动态设置当前选中项:

ui->comboBox->setCurrentIndex(1);//从0开始

设置某一行文本:

ui->conboBox->setItemText(某一行,"12123");


五、ListWidget列表控件

添加数据项:累加

QListWidgetItem *item = new QListWidgetItem("数据");

item->setCheckState(Qt::Unchecked);

ui->listWidget->addItem(item);

获取列表列数:

ui->listWidget->count();

判断某项选中:

if(ui->listWidget->item(某一项)->checkState() == Qt::Checked)

获取某一项数据:

ui->listWidget->item(某一项)->text();


六、TableWidget表格控件

设置行号:

ui->tableWidget->setRowCount(10);

设置列号:

ui->tableWidget->setColumnCount(10);

获取光标所在行号:

int sum = ui->tableWidget->currentRow();//第一行为0

插入行:

ui->tableWidget->insertRow(sum+1);//在光标位置下面插入一行

删除行:

ui->tableWidget->removeRow(sum);//删除光标所在行sum


setAlternatingRowColors()
            setselectionbehavior()
            setsectionresizemode()
            rowcount()
            columncount()
            item()
            showrow()
            hiderow()

七、状态栏

状态栏上消息显示有三种:临时消息、正常消息和永久消息。

临时消息和正常消息都在左侧显示,容易被覆盖。永久消息在右侧显示,不会被覆盖。

临时消息:

ui->statusBar->showMessage(tr("临时消息"));

ui->statusBar->showMessage(tr("临时消息"),2000);//显示2秒钟

正常消息:

(需要添加标签控件,代码添加)

先在文件中添加类的前置声明:class QLabel;
然后添加一个私有对象定义:QLabel *statusLabel;
下面到cpp文件中,先添加头文件包含:#include <QLabel>

然后到构造函数中将前面添加的:
statusLabel = new QLabel;
statusLabel->setMinimumSize(150, 20); // 设置标签最小大小
statusLabel->setFrameShape(QFrame::WinPanel); // 设置标签形状
statusLabel->setFrameShadow(QFrame::Sunken); // 设置标签阴影
ui->statusBar->addWidget(statusLabel);
statusLabel->setText(tr("状态栏正常消息"));
永久消息:

使用addPermanentWidget()函数来添加一个如QLabel一样的可以显示信息的部件

构造函数中添加

QLabel *permanent = new QLabel;
permanent->setFrameStyle(QFrame::Box | QFrame::Sunken);

permanent->setText(

  tr("状态栏永久消息"));

permanent->setTextFormat(Qt::RichText);
permanent->setOpenExternalLinks(true);
ui->statusBar->addPermanentWidget(permanent);