【问题标题】:connect QPushButton and QComboBox连接 QPushButton 和 QComboBox
【发布时间】:2015-07-23 20:05:41
【问题描述】:

如何连接QPushButtonQComboBox

我创建了一个 SLOT,它接受 2 个参数、一个指向 QComboBox 的指针和所选项目的索引:

void modificaExp::eliminaExp(QComboBox *combo,int value)
{
   ......
    combo->removeItem(value);
   ....
}

那里是最宽的:

QComboBox* combo=new QComboBox();
combo->addItem("ciao1");
combo->addItem("ciao44");
combo->addItem("ciao222");
combo->addItem("ciao555");

QPushButton* delButton=new QPushButton();
delButton->setText("delete");

   connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp(combo,combo->currentIndex() )));

所以,当我点击delButton 时,元素会停留在那里。我认为connect命令有问题,具体来说我认为slot没有被调用。

【问题讨论】:

  • connect 不能以这种方式工作。如果您使用的是 Qt4,则必须以某种方式将 combobox 传递给插槽(使其成为类成员,或通过 setProperty)。如果您使用的是 Qt5,您可以切换到 connect 的“新”版本并编写一个 lambda。
  • 谢谢,你是对的!!你保存了我的项目:D

标签: qt qcombobox qpushbutton


【解决方案1】:

你确定你需要这个带有两个参数的插槽吗?

另一种简单的方法:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this);

    connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(deleteSlot()));

}

void MainWindow::deleteSlot()
{
    comboBox->removeItem(comboBox->currentIndex());
}

【讨论】:

    【解决方案2】:
    1. 槽的类型应与信号相同且参数个数等于或少于信号

    2. 在标题中声明 QComboBox 和 QPushButton 对象 修改exp.h

       private:
       QComboBox* combo;
       QPushButton* delButton;
      
    3. modificaexp.cpp

      combo=new QComboBox();
      combo->addItem("ciao1");
      combo->addItem("ciao44");
      combo->addItem("ciao222");
      combo->addItem("ciao555");
      
      delButton=new QPushButton();
      delButton->setText("delete");
      
      connect(delButton, SIGNAL(clicked()), this, SLOT( eliminaExp()));
      
    4. 修改槽

       void modificaExp::eliminaExp()
       {           
          combo->removeItem(combo->currentIndex());        
       }        
      
    5. 参考Qt信号槽documentation

    【讨论】:

    • 我建议的方式相同 =)
    • @t3ft3l--i。在我提交之前,我意识到有一个答案。为你+1!! :)
    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多