我有三个具有三个相似选项的组合框,“一”、“二”、“三”,我想防止在不同的组合框中出现相同的选项。
例子:
组合框1:'一个',
所以当我在 combobox2 和 combobox3 中选择时,只有“二”和“三”可供选择。
我知道我可以结合使用 for 循环和 if 来做到这一点,但是任何人都可以帮我解决一些我可以在这里使用的技巧吗?
只使用一个组合框怎么样?只有六种可能的选择:
用户只使用一个组合框而不是使用三个可用选项不断变化的组合框会容易得多。
编写您自己的类MyComboBox
,该类派生自QComboBox
.
在其中实现一个插槽MyComboBox
将执行以下操作:
void excludeIndex(const QString & text)
{
// Get the list of all the options for the ComboBox
QStringList list = getIndices();
// Remove one (the only) item 'text' from the list
list.removeOne( text );
// Clear the ComboBox and fill with the new values
this->clear();
this->addItems( list );
}
在您的父小部件/应用程序中,将void currentIndexChanged(const QString & text)
来自每个 'sending'MyComboBox
的信号连接QComboBox
到 'receiving' 的这个插槽MyComboBox
。
如果您需要从多个其他ComboBox
es 中排除值,那么在 parent 中实现 slot 可能会更好Widget
。这样,您可以遍历所有“接收” ComboBox
。ComboBox
您将读取其他 es 的所有当前值并ComboBox
从中删除这些值list
。您在父级中的插槽Widget
将不再需要输入QString
。
这是一种解决方案。
基本上,它使用所有项目初始化所有框,并且当框的当前文本发生更改时,它会从其他框中删除该文本。如果框包含的先前文本不是空白的,它会将其添加回所有其他框。
如果您想用超过 8 个来测试它,m_numOfBoxes
请将 mainwindow.cpp 中的变量更改为其他值。
主文件
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QComboBox>
#include <QList>
#include <QStringList>
#include <QMap>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void on_comboBox_changed();
private:
QList <QComboBox *> m_boxes;
QMap <QComboBox *, QString> m_previousText;
int m_numOfBoxes;
QStringList m_allItems;
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QVBoxLayout * vbox = new QVBoxLayout();
m_numOfBoxes = 8;
m_allItems << "";
for(int i = 0; i< m_numOfBoxes; i++)
{
m_allItems << QString::number(i+1);
}
for(int i = 0; i< m_numOfBoxes; i++)
{
QComboBox * temp = new QComboBox;
QObject::connect(temp, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboBox_changed()));
vbox->addWidget(temp);
temp->addItems(m_allItems);
m_boxes.append(temp);
m_previousText[temp] = "";
}
this->setCentralWidget(new QWidget());
this->centralWidget()->setLayout(vbox);
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_comboBox_changed()
{
QComboBox * editedBox = qobject_cast <QComboBox *> (QObject::sender());
QString currentText = editedBox->currentText();
if(currentText == m_previousText[editedBox])
{
return;
}
foreach(QComboBox * box, m_boxes)
{
if(box == editedBox)
{
continue;
}
if(currentText != "")
{
// remove the current text from the other boxes
int index = box->findText(currentText);
if(index != -1)
{
box->removeItem(index);
}
}
if(m_previousText[editedBox] != "")
{
// add the previous text back into the lists for the other boxes
box->addItem(m_previousText[editedBox]);
qDebug() << "Adding back" << m_previousText[editedBox];
}
}
m_previousText[editedBox] = currentText;
}
请注意,当一个项目被添加回一个盒子的列表时,它只是被添加到列表的末尾,所以随着时间的推移,你的项目的顺序可能会被打乱。
希望有帮助。