Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分组QCOMBOX_C++_Qt_Qcombobox - Fatal编程技术网

C++ 分组QCOMBOX

C++ 分组QCOMBOX,c++,qt,qcombobox,C++,Qt,Qcombobox,你能给我一个简单的例子说明如何在Qt中创建这个组合框吗 您可以在此处找到如何执行此操作的说明: 其思想是添加父项和子项,然后在自定义委托的帮助下对它们进行不同的绘制 也就是说,你设置了 item->setData( "parent", Qt::AccessibleDescriptionRole ); 添加组和的父项时 item->setData( "child", Qt::AccessibleDescriptionRole ); 否则 然后使用这些信息进行绘制: if ( ty

你能给我一个简单的例子说明如何在Qt中创建这个组合框吗


您可以在此处找到如何执行此操作的说明:

其思想是添加父项和子项,然后在自定义委托的帮助下对它们进行不同的绘制

也就是说,你设置了

item->setData( "parent", Qt::AccessibleDescriptionRole );
添加组和的父项时

item->setData( "child", Qt::AccessibleDescriptionRole );
否则

然后使用这些信息进行绘制:

if ( type == QLatin1String( "parent" ) ) {
        QStyleOptionViewItem parentOption = option;
        parentOption.state |= QStyle::State_Enabled;
        QItemDelegate::paint( painter, parentOption, index );
    } 
    else if ( type == QLatin1String( "child" ) ) {
        QStyleOptionViewItem childOption = option;
        int indent = option.fontMetrics.width( QString( 4, QChar( ' ' ) ) );
        childOption.rect.adjust( indent, 0, 0, 0 );
        childOption.textElideMode = Qt::ElideNone;
        QItemDelegate::paint( painter, childOption, index );
    }

这是我的简单示例。如果有人需要。 此外,我还添加了具有相同功能的listview。

comboboxdelegate.h:

#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H

#include <QItemDelegate>
#include <QPainter>

class ComboBoxDelegate : public QItemDelegate
{
    Q_OBJECT
    public:
        explicit ComboBoxDelegate(QObject *parent = nullptr);
    protected:
        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
        QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    };

    #endif // COMBOBOXDELEGATE_H
main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
ComboBoxDelegate.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ComboBoxDelegate
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    comboboxdelegate.cpp

HEADERS  += mainwindow.h \
    comboboxdelegate.h

FORMS    += mainwindow.ui
mainwindow.ui:

<?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>345</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QComboBox" name="comboBox"/>
    </item>
    <item row="1" column="0">
     <widget class="QListView" name="listView"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>23</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>

主窗口
0
0
400
345
主窗口
0
0
400
23
顶部工具栏区域
假的

我认为您应该使用
QComboBox::setView()
将树视图设置为组合框视图。该链接不可用。您可以使用wayback machine://检查链接格式是否错误。点击链接不起作用。只需复制并粘贴到浏览器上即可。谢谢。请解释为什么您需要
QString(4,QChar(“”))
在代理中以及设置项目文本时,不需要“QString(4,QChar(“”))。第一个是文本前的空格,第二个是文本后的空格。第二,绝对过剩。最初有方括号(例如:“[one]”)。我编辑了源代码-删除第二个。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "comboboxdelegate.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::start() {

    QStandardItemModel * model = new QStandardItemModel;

    addParentItem(model, "Success");
    addChildItem(model, "one", 1);
    addChildItem(model, "two", 2);
    addChildItem(model, "three", 3);
    addParentItem(model, "Failed");
    addChildItem(model, "one", 1);
    addChildItem(model, "two", 2);
    addChildItem(model, "three", 3);

    ui->comboBox->setModel(model);
    ui->comboBox->setItemDelegate(new ComboBoxDelegate);

    ui->listView->setModel(model);
    ui->listView->setItemDelegate(new ComboBoxDelegate);
}

void MainWindow::addParentItem( QStandardItemModel * model, const QString& text )
{
    QStandardItem* item = new QStandardItem( text );
    item->setFlags( item->flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
    item->setData( "parent", Qt::AccessibleDescriptionRole );
    QFont font = item->font();
    //font.setBold( true );
    font.setItalic( true );
    item->setFont( font );
    model->appendRow( item );
}

void MainWindow::addChildItem( QStandardItemModel * model, const QString& text, const QVariant& data )
{
    QStandardItem* item = new QStandardItem( text );
    item->setData( data, Qt::UserRole );
    item->setData( "child", Qt::AccessibleDescriptionRole );
    model->appendRow( item );
}
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ComboBoxDelegate
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    comboboxdelegate.cpp

HEADERS  += mainwindow.h \
    comboboxdelegate.h

FORMS    += mainwindow.ui
<?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>345</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QComboBox" name="comboBox"/>
    </item>
    <item row="1" column="0">
     <widget class="QListView" name="listView"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>23</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>