Eşya kutuları olarak onay kutularında basit bir combobox yazdım. Bir maddeyi seçip düğmesine bastığımda, hangi öğeyi seçtiğimi gösterir. Ama ben böyle bir şey yapmak isterim: "all" adında bir öğe var - onu seçtiğimde, diğer tüm öğeler seçilmeli ve seçimini kaldırdığımda, diğer tüm öğeler seçilmemiş olmalıdır. Herhangi bir fikir?İçinde Checkbox'lar içeren ComboBox, C++ Qt, tüm onay kutularını işaretleyin/işaretini kaldırın
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
#include <QComboBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
private slots:
void buttonclicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->model = new QStandardItemModel(4, 1); // 4 rows, 1 col
for (int r = 0; r < 4; ++r)
{
QStandardItem* item;
if(r == 0)
item = new QStandardItem(QString("All"));
else
item = new QStandardItem(QString("Item %0").arg(r));
item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item->setData(Qt::Unchecked, Qt::CheckStateRole);
model->setItem(r, 0, item);
}
ui->comboBox->setModel(model);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(buttonclicked()));
}
void MainWindow::buttonclicked()
{
unsigned int i;
for(i=0; i<ui->comboBox->count(); i++)
{
QModelIndex index = ui->comboBox->model()->index(i, 0);
QVariant v = index.data(Qt::CheckStateRole);
int j = v.toInt();
if(j == 2)
{
QModelIndex ii = ui->comboBox->model()->index(i, 0);
QString text = ii.data(Qt::DisplayRole).toString();
qDebug() << text;
}
}
qDebug()<<"";
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
ui dosyası
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>100</x>
<y>50</y>
<width>201</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>110</x>
<y>100</y>
<width>181</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Check what was selected</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
Bu şuna benzer:
düğme için değil, onay kutusu için değil? –
Doğru. Eylem, 'all' düğmesine tıklandığında, açılan kutunun içeriğine ne yapacağınızdır. – UpAndAdam
oh bir düğme demek istemiyorsunuz, yani "all" adlı combobox içinde bir literal öğeye sahip olmak mı istiyorsunuz? Hayatınızı kolaylaştıracağım ve açılan kutuda bulunan bir düğmenin tıklanması, eğer toggeled ise ya tümleri seçecek ya da kutudaki tüm öğeleri seçemeyeceksiniz.Çok daha kolay ve combobox içinde tüm öğeleri öğretmek zorunda kalmadan diğer öğelerin nasıl yönetileceğini öğrenmek zorunda kalmamakta, genellikle – UpAndAdam