「1」、「2」、「3」の 3 つの類似したオプションを持つ 3 つのコンボボックスがあり、別のコンボボックスで同じオプションが使用されないようにしたいと考えています。
例:
コンボボックス 1 : '1',
したがって、コンボボックス 2 とコンボボックス 3 で選択すると、「2」と「3」しか選択できません。
for ループと if を組み合わせてこれを実行できることはわかっていますが、ここで使用できるトリックを教えてくれる人はいますか?
コンボボックスを 1 つだけ使用するのはどうですか? 可能なオプションは 6 つだけです。
使用可能なオプションが絶えず変化する 3 つのコンボボックスを使用する代わりに、1 つのコンボボックスのみを使用する方が、ユーザーにとってはるかに簡単です。
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)
各「送信」MyComboBox
またはQComboBox
「受信」のこのスロットからのシグナルを接続しMyComboBox
ます。
他の複数の es から値を除外する必要がある場合ComboBox
は、親にスロットを実装することをお勧めしますWidget
。そうすれば、すべての「受信」をループできますComboBox
。他のesComboBox
の現在の値をすべて読み取り、ComboBox
それらの値を から削除しますlist
。親のスロットはもうWidget
入力を必要としQString
ません。
これが1つの解決策です。
基本的に、すべてのボックスをすべてのアイテムで初期化し、ボックスの現在のテキストが変更されると、そのテキストを他のボックスから削除します。ボックスに含まれていた前のテキストが空白でない場合は、他のすべてのボックスに追加されます。
8 つ以上でテストしたい場合はm_numOfBoxes
、mainwindow.cpp の変数を別の値に変更してください。
main.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;
}
アイテムがボックスのリストに再度追加されると、リストの最後に追加されるだけなので、時間の経過とともにアイテムの順序が乱れる可能性があることに注意してください。
それが役立つことを願っています。