连接三个组合框以控制qt中可用的选项
我有三个组合框,有三个相似的选项,“一”、“二”、“三”,我想在不同的组合框中阻止相同的选项 例如: 组合框1:'一' 所以当我在combobox2和Combox3中选择时,只有“2”和“3”可供选择连接三个组合框以控制qt中可用的选项,qt,combobox,Qt,Combobox,我有三个组合框,有三个相似的选项,“一”、“二”、“三”,我想在不同的组合框中阻止相同的选项 例如: 组合框1:'一' 所以当我在combobox2和Combox3中选择时,只有“2”和“3”可供选择 我知道我可以用for循环和if组合来实现这一点,但是有人能帮我一些技巧吗?只使用一个组合框怎么样?只有六种可能的选择: 一二三 一三二 二一三 二三一 三一二 三二一 如果用户只使用一个组合框,而不是使用三个可用选项不断变化的组合框,则会更加容易。编写您自己的类MyComboBox,该类派生自QC
我知道我可以用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 );
}
在父窗口小部件/应用程序中,将来自每个“发送”MyComboBox或QComboBox
的信号void currentIndexChanged(const QString&text)
连接到“接收”MyComboBox的此插槽
如果需要从多个其他组合框
中排除这些值,那么最好在父小部件
中实现该插槽。这样,您就可以循环所有的“接收”组合框。根据组合框
您将读取其他组合框
的所有当前值,并从列表
中删除这些值。您在父小部件中的插槽将不再需要输入QString
。这里有一个解决方案
基本上,它使用所有项目初始化所有框,当框的当前文本发生更改时,它将从其他框中删除该文本。如果框中包含的上一个文本不是空的,它会将其添加回所有其他框中
如果您想用8以上的值来测试它,请将mainwindow.cpp中的变量m_numoboxs
更改为其他值
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#包括
#包括“mainwindow.h”
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
w、 show();
返回a.exec();
}
主窗口
#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
\ifndef主窗口
#定义主窗口
#包括
#包括
#包括
#包括
#包括
类主窗口:公共QMainWindow
{
Q_对象
公众:
主窗口(QWidget*父窗口=0);
~main窗口();
公众时段:
组合框上的无效内容已更改();
私人:
QList m_盒;
QMap m_先前的文本;
国际货币基金组织;
QStringList m_allItems;
};
#endif//main窗口
mainwindow.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;
}
#包括“mainwindow.h”
#包括
#包括
主窗口::主窗口(QWidget*父窗口)
:QMainWindow(父级)
{
QVBoxLayout*vbox=新的QVBoxLayout();
m_numobxes=8;
m_allItems addItems(m_allItems);
m_盒。附加(临时);
m_previousText[temp]=“”;
}
此->setCentralWidget(新的QWidget());
此->centralWidget()->setLayout(vbox);
}
MainWindow::~MainWindow()
{
}
void main window::on_comboBox_changed()
{
QComboBox*editedBox=qobject_cast(qobject::sender());
QString currentText=editedBox->currentText();
如果(currentText==m_previousText[EditeBox])
{
返回;
}
foreach(QCOMBOX*盒、m_盒)
{
如果(框==编辑框)
{
继续;
}
如果(currentText!=“”)
{
//从其他框中删除当前文本
int index=box->findText(当前文本);
如果(索引!=-1)
{
框->删除项(索引);
}
}
如果(m_previousText[EditeBox]!=“”)
{
//将前面的文本添加回其他框的列表中
框->添加项(m_previousText[editedBox]);
qDebug()问题是我有8个组合框,每个组合框有6个选项。我举的例子是为了简化问题。显然,这8个组合框都不是独占的,或者用户不能在最后2个组合框中选择任何内容。看在上帝的份上,这是组合框,不是组合框:)