0

「1」、「2」、「3」の 3 つの類似したオプションを持つ 3 つのコンボボックスがあり、別のコンボボックスで同じオプションが使用されないようにしたいと考えています。

例:

コンボボックス 1 : '1',

したがって、コンボボックス 2 とコンボボックス 3 で選択すると、「2」と「3」しか選択できません。

for ループと if を組み合わせてこれを実行できることはわかっていますが、ここで使用できるトリックを教えてくれる人はいますか?

4

3 に答える 3

1

コンボボックスを 1 つだけ使用するのはどうですか? 可能なオプションは 6 つだけです。

  1. 一二三
  2. 1 3 2
  3. 2 1 3
  4. 二三一
  5. 3 1 2
  6. スリーツーワン

使用可能なオプションが絶えず変化する 3 つのコンボボックスを使用する代わりに、1 つのコンボボックスのみを使用する方が、ユーザーにとってはるかに簡単です。

于 2013-02-04T05:48:46.307 に答える
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ません。

于 2013-02-04T14:30:54.573 に答える
0

これが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;
}

アイテムがボックスのリストに再度追加されると、リストの最後に追加されるだけなので、時間の経過とともにアイテムの順序が乱れる可能性があることに注意してください。

それが役立つことを願っています。

于 2013-02-04T17:50:32.710 に答える