• 10404阅读
  • 2回复

【提问】对于QComboBox,不知有没有什么好办法动态生成列表内容 [复制链接]

上一主题 下一主题
离线yfy002
 

只看楼主 倒序阅读 楼主  发表于: 2005-08-09
在qt的文档里没有看到清除QComboBox列表内容的方法。
目前我的做法是
首先setMaxCount(0);
一些操作
setMaxCount(num)
这样就把列表框清空了,然后根据需要再insert进去,不知有没有什么别的好方法??
[ 此贴被XChinux在2005-08-09 21:55重新编辑 ]
我渴望平静,风却给了我涟漪
我的blog:
http://sungaoyong.cublog.cn
离线XChinux

只看该作者 1楼 发表于: 2005-08-09
确实没提供一个合适的接口.最方便的话,是使用QStringList最方便了.
main.cpp
#include "dlg.h"
int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    myDialog *dlg = new myDialog();
    return dlg->exec();
}


dlg.h
#ifndef _DLG_H_
#define _DLG_H_

#include <QtGui/QtGui>
#include "ui_combobox.h"

class myDialog:public QDialog
{
    Q_OBJECT
public:
    myDialog();
    ~myDialog();
private slots:
    void buttonInsert();
    void buttonDelete();
    void comboBoxSelect(QString);
    void comboBoxSelect(int);
private:
    Ui::Form *ui;    
};

#endif //_DLG_H_


dlg.cpp
#include "dlg.h"

myDialog::myDialog()
{
    ui = new Ui::Form();
    ui->setupUi(this);
    connect(ui->PushButtonInsert, SIGNAL(clicked()), this, SLOT(buttonInsert()));
    connect(ui->PushButtonDelete, SIGNAL(clicked()), this, SLOT(buttonDelete()));
    connect(ui->ComboBoxTest, SIGNAL(activated(QString)), this, SLOT(comboBoxSelect(QString)));
    connect(ui->ComboBoxTest, SIGNAL(activated(int)), this, SLOT(comboBoxSelect(int)));
}

myDialog::~myDialog()
{
}

void myDialog::buttonInsert()
{
    //QMessageBox::information(this, tr("Information"), tr("Alert"), QMessageBox::Ok);
    ui->ComboBoxTest->clear();
    ui->ComboBoxTest->addItems(QStringList()<<"One"<<"Two"<<"Three"<<"Four"<<"Five");
}

void myDialog::buttonDelete()
{
    //QMessageBox::information(this, QString::number(ui->ComboBoxTest->currentIndex()), ui->ComboBoxTest->currentText(), QMessageBox::Ok);
    ui->ComboBoxTest->removeItem(ui->ComboBoxTest->currentIndex());
}

void myDialog::comboBoxSelect(QString strText)
{
    setWindowTitle("You selected: " + strText);
}

void myDialog::comboBoxSelect(int iIndex)
{
    setWindowTitle("You selected: " + ui->ComboBoxTest->itemText(iIndex));
}


combobox.ui
<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>Form</class>
<widget class="QWidget" name="Form" >
<property name="geometry" >
  <rect>
  <x>0</x>
  <y>0</y>
  <width>387</width>
  <height>128</height>
  </rect>
</property>
<property name="windowTitle" >
  <string>Form</string>
</property>
<widget class="QPushButton" name="PushButtonInsert" >
  <property name="geometry" >
  <rect>
  <x>10</x>
  <y>90</y>
  <width>75</width>
  <height>23</height>
  </rect>
  </property>
  <property name="text" >
  <string>Insert</string>
  </property>
</widget>
<widget class="QPushButton" name="PushButtonDelete" >
  <property name="geometry" >
  <rect>
  <x>90</x>
  <y>90</y>
  <width>75</width>
  <height>23</height>
  </rect>
  </property>
  <property name="text" >
  <string>Delete</string>
  </property>
</widget>
<widget class="QPushButton" name="PushButtonClear" >
  <property name="geometry" >
  <rect>
  <x>170</x>
  <y>90</y>
  <width>75</width>
  <height>23</height>
  </rect>
  </property>
  <property name="text" >
  <string>Clear</string>
  </property>
</widget>
<widget class="QComboBox" name="ComboBoxTest" >
  <property name="geometry" >
  <rect>
  <x>20</x>
  <y>30</y>
  <width>351</width>
  <height>22</height>
  </rect>
  </property>
</widget>
<widget class="QPushButton" name="PushButtonQuit" >
  <property name="geometry" >
  <rect>
  <x>300</x>
  <y>90</y>
  <width>75</width>
  <height>23</height>
  </rect>
  </property>
  <property name="text" >
  <string>Quit</string>
  </property>
</widget>
</widget>
<pixmapfunction></pixmapfunction>
<resources/>
<connections>
<connection>
  <sender>PushButtonQuit</sender>
  <signal>clicked()</signal>
  <receiver>Form</receiver>
  <slot>close()</slot>
  <hints>
  <hint type="sourcelabel" >
  <x>320</x>
  <y>96</y>
  </hint>
  <hint type="destinationlabel" >
  <x>290</x>
  <y>73</y>
  </hint>
  </hints>
</connection>
<connection>
  <sender>PushButtonClear</sender>
  <signal>clicked()</signal>
  <receiver>ComboBoxTest</receiver>
  <slot>clear()</slot>
  <hints>
  <hint type="sourcelabel" >
  <x>207</x>
  <y>97</y>
  </hint>
  <hint type="destinationlabel" >
  <x>205</x>
  <y>37</y>
  </hint>
  </hints>
</connection>
</connections>
</ui>


下面是源代码:
[ 此贴被XChinux在2005-08-10 01:36重新编辑 ]
附件: ex27.rar (32 K) 下载次数:29
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线yfy002

只看该作者 2楼 发表于: 2005-08-23
原来是有函数的阿,呵呵

4.0.1
- QComboBox
    Made removeItem() and setRootModelIndex() visible in the
    documentation.
我渴望平静,风却给了我涟漪
我的blog:
http://sungaoyong.cublog.cn
快速回复
限100 字节
 
上一个 下一个