QComboBox 中 activated信号与 currentIndexChanged信号的区别;及 items count,基本用法

QComboBox中的activated信号在用户选择item时发送,无论选择是否改变,而currentIndexChanged信号在currentIndex改变时发送,包括用户交互和程序修改。currentIndexChanged会在激活后先发送。

QComboBox中activated信号与currentIndexChanged信号的区别

QT ComBoBox的基本方法

========================================

   ///
    int cnt =ui->comboBox_abc->maxCount();
    int cntxx =ui->comboBox_abc->count();
    qDebug()<<"comboBox maxCount: "<<cnt;
    qDebug()<<"comboBox count: "<<cntxx;

输出:

comboBox maxCount:  2147483647  //Qt系统设定的总条目个数?
comboBox count:  5 //在用的总条目个数

最大索引序号 =最大索引个数 -1  //起始索引为0

=============================

QT ComBoBox的基本方法

    1)addItems

    void addItem(const QString &text, const QVariant &userData = QVariant())

    void addItem(const QIcon &icon, const QString &text, const QVariant &userData = QVariant())

    在列表的最后一项添加一个文本内容为test选项

    2)currentText

    QString currentText() const

    返回下拉列表框中当前选中的文本

    3)count

    int count() const

    返回当前列表框中选项数量

    4)currentIndex

    int currentIndex() const

    返回当前列表框中选中文本的序号

          用法:int  num  =  this->ui- ComBoBox->currentIndex();

    5)setCurrentIndex(int num);//num  是选中的序号

    设置当 ComBoBox折叠后它显示的值。

    6)insertItem( , ,)

        insertItem(位置,文本,数据)

         ui->comBox->insertItem(1,"abc","apple");
         
            ui->comBox->currentIndex(); //1
            ui->comBox->currentText(); //abc
            ui->comBox->currentData(); //apple
            //可以动态取值

        ui->comboBox_abc->maxCount();

        ui->comboBox_abc->count();

————————————————
版权声明:本文为CSDN博主「-点点-」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Dian0dian0/article/details/97775907

QComboBox中activated信号与currentIndexChanged信号的区别

在使用中可能会有类似的需求,改变ComboBox的当前选中值时发出消息信号,但是QComboBox提供了两小类信号,它们有什么区别呢?

activated(int)

    This signal is sent when the user chooses an item in the combobox. The item’s index is passed. Note that this signal is sent even when the choice is not changed.
    翻译:当用户选则ComboBox中的Item时,将会产生一个选中Item序号值的消息信号,不管选项是够改变都会产生该消息。

activated(QString)

    This signal is sent when the user chooses an item in the combobox. The item’s text is passed. Note that this signal is sent even when the choice is not changed.
    翻译:同理,只不过传送的是选中Item的ItemName。

currentIndexChanged (int)

    This signal is sent whenever the currentIndex in the combobox changes either through user interaction or programmatically. The item’s index is passed or -1 if the combobox becomes empty or the currentIndex was reset.
    翻译:当前ComboBox中的CurrentIndex改变时,不管是交互式还是通过程序改变选项,都会产生一个改变值得消息信号,如果ComboBox为空或重置当前ComboBox,产生的消息值返回-1。

currentIndexChanged (QString)

    This signal is sent whenever the currentIndex in the combobox changes either through user interaction or programmatically. The item’s text is passed.
    翻译:翻译:同理,只不过传送的是选中Item的ItemName

currentIndexChanged

重点是currentIndexChanged不管是交互式(通过鼠标点击切换)还是通过程序改变选项,都会产生一个改变值的消息信号,以下是测试代码:

/****************************************************************
 Author :   BingLee
 Date   :   2019-07-30
 Info   :
 https://blog.csdn.net/Bing_Lee (C)All rights reserved.
******************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->comboBox->addItem("111");
    ui->comboBox->addItem("222");
    ui->comboBox->addItem("333");
    ui->comboBox->addItem("444");
    connect(ui->comboBox, SIGNAL(activated(int)), this, SLOT(CheckActiveSignal()));
    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(CheckIndexChangedSignal()));
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(ComboboxSelectedPlus()));
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::ComboboxSelectedPlus()
{
    //此处绑定按钮的动作,点击按钮时,设置当前选中ItemNum增加1,
    //改变currentIndex又会触发CheckIndexChangedSignal函数,最前边有Connect消息。
    ui->comboBox->setCurrentIndex(ui->comboBox->currentIndex()+1);
}

 
void MainWindow::CheckActiveSignal()
{
    //通过鼠标切换选项会进入该函数
    //通过按钮改变currentIndex不会进入该函数
    int a = 0;
}
void MainWindow::CheckIndexChangedSignal()
{
    //通过鼠标切换选项会进入该函数
    //通过按钮改变currentIndex会进入该函数
    int a = 0;
}
//同时注意到有个小的细节,Connect的信号总是会先发currentIndexChanged,后发activated

————————————————
版权声明:本文为CSDN博主「机器人梦想家」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Bing_Lee/article/details/97810100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值