2013-09-24 11 views
5

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:

enter image description here

cevap

1

böyle tıklandığında/istenen içindeki onay kutularının üzerinde yineleme olacak tetikleyen bu düğme için bir qtslot Uygulamak combobox ve kontrol durumlarını düzenleyin.

+0

düğme için değil, onay kutusu için değil? –

+0

Doğru. Eylem, 'all' düğmesine tıklandığında, açılan kutunun içeriğine ne yapacağınızdır. – UpAndAdam

+0

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

0

Kod altyazısını kullanarak onay kutusu listesini çoğaltmaya çalışırken model alt sınıflarının nasıl kullanıldığını öğrendim ve listeyi dolduran onay kutularının herhangi birinde durum işaretlendiğinde bir sinyal tetiklemenin bir yolunu buldum. MainWindow.cpp yuvayı uygulanması

public slots: 
     void itemCheck(QModelIndex a, QModelIndex b); 

:

void MainWindow::void itemCheck(QModelIndex a, QModelIndex b) 
{ 
    qDebug() << "check event"; 
} 

Ve modelin DataChanged sinyale bu yeni yuvası bağlantı: MainWindow.h bir kamu yuvası bildirme

connect(ui->comboBox->model(), SIGNAL(dataChanged(QModelIndex, QModelIndex, QVector<int>)), this, SLOT(itemCheck(QModelIndex, QModelIndex))); 

Ben ince k, olayı "Tümü" onay kutusuna göre filtrelemek ve diğerlerini uygun şekilde ayarlamak mümkündür. Çalıştırmak için Yönetilen:

void MainWindow::itemCheck(QModelIndex a, QModelIndex b) 
{ 
    qint16 i; 
    QModelIndex cycleIndex; 
    // if "All" checked 
    if (a.row() == 0) 
    { 
     // blocking new dataChanged singnal while managing other checkboxes 
     ui->comboBox->model()->blockSignals(true); 
     // setting other checkboxes appropriately 
     if (a.data(Qt::CheckStateRole).toBool()) 
     { 
      for (i = 1; i<ui->comboBox->count(); i++) 
      { 
       cycleIndex = ui->comboBox->model()->index(i, 0); 
       ui->comboBox->model()->setData(cycleIndex, Qt::Checked, Qt::CheckStateRole); 
      } 
     } else 
     { 
      for (i = 1; i<ui->comboBox->count(); i++) 
      { 
       cycleIndex = ui->comboBox->model()->index(i, 0); 
       ui->comboBox->model()->setData(cycleIndex, Qt::Unchecked, Qt::CheckStateRole); 
      } 
     } 
     ui->comboBox->model()->blockSignals(false); 
    } 
} 

Umut bu sıfırdan Qt öğrenme birisi yardımcı olacaktır.

İlgili konular