Qt 可编辑下拉菜单(QComboBox) demo
1、创建并初始化一个QComboBox对象
在[0]索引位置上设置一个特殊的标记值custom表示可以自定义输入数据
comboBox2 = new QComboBox(this);
comboBox2->setGeometry(100,100,200,100);
QStringList params;
params<<"custom"<<"5600"<<"57600"<<"115200"<<"128000"<<"256000"<<"921600";
comboBox2->addItems(params);//将这些数据加入到comboBox中
comboBox2->setDuplicatesEnabled(true);//设置可以输入重复值(非必须选项)
comboBox2->setCurrentIndex(6);//设置默认选中项
2、为comboBox编写信号槽函数
mainwindow.h:
class MainWindow : public QMainWindow
{
Q_OBJECT
....
private slots:
void on_comboBox_currentIndexChangedIndex(int index);
void on_comboBox_currentIndexChangedText(const QString &arg1);
....
};
mainwindow.cpp:
void MainWindow::on_comboBox_currentIndexChangedIndex(int index)
{
//判断是否选中[0]索引项custom
//是则使能编辑、设置当前框内为空字符
//否则禁能编辑,只能选择而不可编辑
if(index == 0){
comboBox2->setEditable(true);
comboBox2->setCurrentText("");
}else{
comboBox2->setEditable(false);
}
}
void MainWindow::on_comboBox_currentIndexChangedText(const QString &arg1)
{
qDebug()<<"选择"<<arg1;
}
3、绑定槽函数
由于QComboBox::currentIndexChanged是重载方法,参数不同导致Qt5语法不能正确绑定,所以使用下列方法进行绑定
mainwindow.cpp:
connect(comboBox2,static_cast<void (QComboBox::*)(int index)>(&QComboBox::currentIndexChanged),this,&MainWindow::on_comboBox_currentIndexChangedIndex);
connect(comboBox2,static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),this,&MainWindow::on_comboBox_currentIndexChangedText);
完整代码:
mainwindow.h:文章来源:https://www.toymoban.com/news/detail-403101.html
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QMainWindow>
#include <QString>
#include <QDebug>
#include <QLabel>
#include <QPushButton>
#include <QObject>
#include <QByteArray>
#include <QComboBox>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QComboBox * comboBox2;
private slots:
void on_comboBox_currentIndexChangedIndex(int index);
void on_comboBox_currentIndexChangedText(const QString &arg1);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:文章来源地址https://www.toymoban.com/news/detail-403101.html
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
comboBox2 = new QComboBox(this);
comboBox2->setGeometry(100,100,200,100);
QStringList params;
params<<"custom"<<"5600"<<"57600"<<"115200"<<"128000"<<"256000"<<"921600";
comboBox2->addItems(params);
comboBox2->setDuplicatesEnabled(true);
comboBox2->setCurrentIndex(6);
params.clear();
connect(comboBox2,static_cast<void (QComboBox::*)(int index)>(&QComboBox::currentIndexChanged),this,&MainWindow::on_comboBox_currentIndexChangedIndex);
connect(comboBox2,static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),this,&MainWindow::on_comboBox_currentIndexChangedText);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_comboBox_currentIndexChangedIndex(int index)
{
if(index == 0){
comboBox2->setEditable(true);
comboBox2->setCurrentText("");
//comboBox2->setItemText(0,"");
}else{
comboBox2->setEditable(false);
//comboBox2->setItemText(0,"custom");
}
}
void MainWindow::on_comboBox_currentIndexChangedText(const QString &arg1)
{
qDebug()<<"选择"<<arg1;
}
到了这里,关于Qt 可编辑下拉菜单(QComboBox) demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!