1.相关说明
combobox中item的文字、data、图片设置
2.界面绘制
3.相关主要代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
// 清空列表
void Widget::on_btnClear_clicked()
{
ui->plainTextEdit->clear();
}
// 初始化列表
void Widget::on_btnInitCities_clicked()
{
QIcon icon;
icon.addFile(":/png/images/chengshi.png");
ui->comboBoxCities->clear();
for(int i = 0; i < 20; i++){
ui->comboBoxCities->addItem(icon, QString("Item%1").arg(i));
}
}
// 清空列表
void Widget::on_btnClearList_clicked()
{
ui->comboBoxCities->clear();
}
// 可编辑
void Widget::on_chkEdit_clicked(bool checked)
{
ui->comboBoxCities->setEditable(checked);
}
// 初始化城市+区号
void Widget::on_btnCitiesNum_clicked()
{
ui->comboxBoxCitiesNum->clear();
QMap<QString, int> city_zone;
city_zone.insert("北京", 1);
city_zone.insert("上海", 2);
city_zone.insert("深圳", 3);
city_zone.insert("成都", 4);
foreach(const QString str, city_zone.keys()){
// ui->comboxBoxCitiesNum->addItem(str, city_zone.value(str));
ui->comboxBoxCitiesNum->addItem(str, city_zone[str]);
}
}
void Widget::on_comboBoxCities_currentTextChanged(const QString &arg1)
{
ui->plainTextEdit->appendPlainText(arg1);
}
void Widget::on_comboxBoxCitiesNum_currentIndexChanged(int index)
{
Q_UNUSED(index);
QString str = ui->comboxBoxCitiesNum->currentText() + "-" + ui->comboxBoxCitiesNum->currentData().toString();
ui->plainTextEdit->appendPlainText(str);
}