1.組合框:?QComboBox
組合框:QComboBox
用于存放一些列表項(xiàng)
實(shí)例化
//實(shí)例化
QComboBox* comboBox = new QComboBox(this);
1.1?代碼實(shí)現(xiàn)
1.1.1?組合框的基本函數(shù)
QComboBox
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
//添加列表項(xiàng)
this->comboBox->addItem("蘋果");
this->comboBox->addItem("香蕉");
this->comboBox->addItem("西瓜");
//添加圖片
this->comboBox->addItem(QIcon(":/img/photo.jpg"),"葡萄");
//一次添加多個(gè)列表項(xiàng)
QStringList list;
list<<"星期一"<<"星期二"<<"星期三";
this->comboBox->addItems(list);
//返回組合框里有多少列表項(xiàng)
qDebug()<<"組合框里列表項(xiàng)數(shù)目"<<this->comboBox->count()<<endl;
//返回當(dāng)前列表項(xiàng)的下標(biāo)
qDebug()<<"返回當(dāng)前列表項(xiàng)的下標(biāo)"<<this->comboBox->currentIndex()<<endl;
//返回當(dāng)前列表項(xiàng)
qDebug()<<"返回當(dāng)前列表項(xiàng)"<<this->comboBox->currentText()<<endl;
//在組合框中查詢是否有指定的列表項(xiàng),如果有就返回下標(biāo),沒有就返回-1
int index = this->comboBox->findText("星期二");
qDebug()<<"星期二對(duì)應(yīng)的下標(biāo):"<<index<<endl;
//通過下標(biāo)找到列表項(xiàng)
QString text = this->comboBox->itemText(index);
qDebug()<<"下標(biāo)為"<<index<<"的列表項(xiàng)是"<<text<<endl;
//通過下標(biāo)找到圖片
QIcon icon = this->comboBox->itemIcon(2);
//在指定位置插入列表項(xiàng)
QStringList list2;
list2<<"昨天"<<"今天"<<"明天";
this->comboBox->insertItems(1,list2);
//移除指定的列表項(xiàng)
this->comboBox->removeItem(1);//這里我移除了 昨天
//將列表項(xiàng)更新為新的列表項(xiàng)
this->comboBox->setItemText(index,"hello");
}
Dialog::~Dialog()
{
delete ui;
}
1.1.2?練習(xí)購(gòu)物車
用組合框模擬賬戶的存儲(chǔ)與取出
使用組合框模擬購(gòu)物車
(1)用戶可以添加物品到購(gòu)物車中
(2)用戶可以移除購(gòu)物車中指定的物品
(3)用戶可以清空并結(jié)算購(gòu)物車中商品總價(jià)
(4)用戶一個(gè)商品用戶可以購(gòu)買多個(gè)
效果如下
QConboBox_gouwuche
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <Qdebug>
#include <QComboBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = nullptr);
~Dialog();
private slots:
void on_xigau_clicked();
void on_car_clicked();
void on_flower_clicked();
void on_del_clicked();
void on_jiesuan_clicked();
private:
Ui::Dialog *ui;
//實(shí)例化組合框
QComboBox* comboBox = new QComboBox(this);
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
//用組合框模擬賬戶的存儲(chǔ)與取出
// 使用組合框模擬購(gòu)物車
// (1)用戶可以添加物品到購(gòu)物車中
// (2)用戶可以移除購(gòu)物車中指定的物品
// (3)用戶可以清空并結(jié)算購(gòu)物車中商品總價(jià)
// (4)用戶一個(gè)商品用戶可以購(gòu)買多個(gè)
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
this->comboBox->move(260,150);
this->comboBox->resize(100,40);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_xigau_clicked()//西瓜
{
QString xg = ui->xigau->text();
QStringList watermelon = xg.split(" ");//以空格分割字符串
this->comboBox->addItem(watermelon[0]);//將物品給組合框選項(xiàng)
}
void Dialog::on_car_clicked()//汽車
{
QString qiche = ui->car->text();
QStringList che = qiche.split(" ");//以空格分割字符串
this->comboBox->addItem(che[0]);
}
void Dialog::on_flower_clicked()//鮮花
{
QString xianhua = ui->flower->text();
QStringList hua = xianhua.split(" ");//以空格分割字符串
this->comboBox->addItem(hua[0]);
}
void Dialog::on_del_clicked()//刪除列表項(xiàng)
{
int index = this->comboBox->currentIndex();
this->comboBox->removeItem(index);
}
void Dialog::on_jiesuan_clicked()//結(jié)算
{
int sum = 0;
int num = this->comboBox->count();//獲取標(biāo)簽中有多少項(xiàng)
for(int i=0;i<num;i++){//循環(huán)遍歷出列表項(xiàng)
QString cur = this->comboBox->itemText(i);//取出列表項(xiàng)
if(cur=="西瓜"){
sum+=12;
}else if(cur=="汽車"){
sum+=25;
}else if(cur=="鮮花"){
sum+=9;
}
}
ui->label->setText(QString::number(sum));//QString::number(sum)轉(zhuǎn)為字符串
comboBox->clear();
}
1.2?信號(hào)
1.2.1?信號(hào)函數(shù)
void activated(int index) //當(dāng)列表項(xiàng)變化時(shí)觸發(fā)該信號(hào)
void activated(const QString &text)
void currentIndexChanged(int index) //當(dāng)前列表項(xiàng)下標(biāo)變化時(shí)觸發(fā)該信號(hào)
void currentIndexChanged(const QString &text)
void currentTextChanged(const QString &text)//當(dāng)前列表項(xiàng)變化時(shí)觸發(fā)該信號(hào)
void editTextChanged(const QString &text)
void highlighted(int index) //列表項(xiàng)高亮?xí)r觸發(fā)該信號(hào)
void highlighted(const QString &text)
QComboBox
綁定
dialog.cpp
//綁定變化列表項(xiàng)信號(hào),即當(dāng)列表項(xiàng)被選中觸發(fā)
connect(this->comboBox,SIGNAL(activated(QString)),this,SLOT(rece1_Text(QString)));
//綁定高亮信號(hào),即鼠標(biāo)放在哪高亮觸發(fā)
connect(this->comboBox,SIGNAL(highlighted(int)),this,SLOT(rece2_Text_Index(int)));
槽
定義
dialog.h
public slots://槽
void rece1_Text(const QString& text);//綁定變化列表項(xiàng)信號(hào),即當(dāng)列表項(xiàng)被選中觸發(fā)
void rece2_Text_Index(int dex);//綁定高亮信號(hào),即鼠標(biāo)放在哪高亮觸發(fā)
實(shí)現(xiàn)
dialog.cpp
void Dialog::rece1_Text(const QString &text){
qDebug()<<"列表項(xiàng)"<<text<<endl;
}
void Dialog::rece2_Text_Index(int dex){
qDebug()<<"下表為"<<dex<<endl;
}
文章來源:http://www.zghlxwxcb.cn/news/detail-819245.html
1.3?UI實(shí)現(xiàn)
方法解釋文章來源地址http://www.zghlxwxcb.cn/news/detail-819245.html
editable:設(shè)置組框當(dāng)前列表項(xiàng)是否可編輯
currentText:獲取組合框中的當(dāng)前顯示的列表項(xiàng)
currrenIdex:獲取當(dāng)前列表項(xiàng)的下標(biāo)
maxVisibleItems:列表項(xiàng)可移動(dòng)的最大數(shù)目
maxCount:設(shè)置組合框中最多能容納的列表項(xiàng)的數(shù)目
insertPolicy:設(shè)置插入列表項(xiàng)的策略
sizeAdjustPolicy:設(shè)置組合框中列表項(xiàng)適應(yīng)組合框
minimumContentsLength:最小的列表項(xiàng)的長(zhǎng)度
iconsize:設(shè)置圖片的大小
placeholderText:背景提示的文本
frame:邊框
modelColumn:列表項(xiàng)
到了這里,關(guān)于使用組合框QComboBox模擬購(gòu)物車的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!