Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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
如何在QT中将QComboBox设置为MultiSelect?_Qt - Fatal编程技术网

如何在QT中将QComboBox设置为MultiSelect?

如何在QT中将QComboBox设置为MultiSelect?,qt,Qt,如何在QT中将QComboBox设置为MultiSelect QT中Combox中没有MultiSelect选项 或 任何人都可以向我推荐一些不同的控件,但外观和感觉应该像QCombobox一样。一种替代方法是将带有可检查操作的菜单设置为按钮,如我所示 或者您可以更改组合的选择模型,并控制弹出窗口的隐藏和显示,如图所示//这是名为CheckBoxList.h的文件 #ifndef CHECKBOXLIST_H #define CHECKBOXLIST_H #include <QtGui&

如何在QT中将QComboBox设置为MultiSelect

QT中Combox中没有MultiSelect选项


任何人都可以向我推荐一些不同的控件,但外观和感觉应该像QCombobox一样。

一种替代方法是将带有可检查操作的菜单设置为按钮,如我所示

或者您可以更改组合的选择模型,并控制弹出窗口的隐藏和显示,如图所示

//这是名为CheckBoxList.h的文件

#ifndef CHECKBOXLIST_H
#define CHECKBOXLIST_H

#include <QtGui>

class CheckBoxList: public QComboBox
{
    Q_OBJECT;

public:
    CheckBoxList(QWidget *widget = 0);
    virtual ~CheckBoxList();
    bool eventFilter(QObject *object, QEvent *event);
    virtual void paintEvent(QPaintEvent *);
    void SetDisplayText(QString text);
    QString GetDisplayText() const;

private:
    QString m_DisplayText;
};

#endif // CHECKBOXLIST_H
\ifndef复选框列表
#定义复选框列表
#包括
类复选框列表:公共QComboBox
{
Q_对象;
公众:
复选框列表(QWidget*widget=0);
虚拟~CheckBoxList();
bool事件过滤器(QObject*对象,QEvent*事件);
虚拟虚空paintEvent(QPaintEvent*);
void SetDisplayText(QString文本);
QString GetDisplayText()常量;
私人:
QString m_显示文本;
};
#endif//复选框列表

//这是名为CheckBoxList.cpp的文件

#include "CheckBoxList.h"
#include <QtGui>

// internal private delegate
class CheckBoxListDelegate : public QItemDelegate
{
public:

    CheckBoxListDelegate(QObject *parent)
         : QItemDelegate(parent)
    {
        ;
    }

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const
    {
        //Get item data
        bool value = index.data(Qt::UserRole).toBool();
        QString text = index.data(Qt::DisplayRole).toString();

        // fill style options with item data
        const QStyle *style = QApplication::style();
        QStyleOptionButton opt;
        opt.state |= value ? QStyle::State_On : QStyle::State_Off;
        opt.state |= QStyle::State_Enabled;
        opt.text = text;
        opt.rect = option.rect;

        // draw item data as CheckBox
        style->drawControl(QStyle::CE_CheckBox,&opt,painter);
//QMessageBox::information(0,"Info",text);

    }

        QWidget *createEditor(QWidget *parent,
         const QStyleOptionViewItem & option ,
         const QModelIndex & index ) const
    {
        // create check box as our editor

         QCheckBox *editor = new QCheckBox(parent);

         return editor;
    }

     void setEditorData(QWidget *editor,
                                         const QModelIndex &index) const
     {

         //set editor data
         QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
         myEditor->setText(index.data(Qt::DisplayRole).toString());
         myEditor->setChecked(index.data(Qt::UserRole).toBool());

//
     }

     void setModelData(QWidget *editor, QAbstractItemModel *model,
                                        const QModelIndex &index) const
     {
         //get the value from the editor (CheckBox)
         QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
         bool value = myEditor->isChecked();


         //set model data
         QMap<int,QVariant> data;
         data.insert(Qt::DisplayRole,myEditor->text());
         data.insert(Qt::UserRole,value);
         model->setItemData(index,data);

     }

     void updateEditorGeometry(QWidget *editor,
         const QStyleOptionViewItem &option, const QModelIndex &index ) const
     {

         editor->setGeometry(option.rect);


     }
 };     
//min-width:10em; 
     CheckBoxList::CheckBoxList(QWidget *widget )
     :QComboBox(widget),m_DisplayText(0)
     {
    // set delegate items view 
    view()->setItemDelegate(new CheckBoxListDelegate(this));
    //view()->setStyleSheet("  padding: 15px; ");
    // Enable editing on items view
    view()->setEditTriggers(QAbstractItemView::CurrentChanged);

    // set "CheckBoxList::eventFilter" as event filter for items view 
    view()->viewport()->installEventFilter(this);


    // it just cool to have it as defualt ;)
    view()->setAlternatingRowColors(true);
     }


CheckBoxList::~CheckBoxList()
{
    ;
}
bool CheckBoxList::eventFilter(QObject *object, QEvent *event)
{
    // don't close items view after we release the mouse button
    // by simple eating MouseButtonRelease in viewport of items view
    if(event->type() == QEvent::MouseButtonRelease && object==view()->viewport()) 
    {
        return true;
    }
    return QComboBox::eventFilter(object,event);
}
void CheckBoxList::paintEvent(QPaintEvent *)
{
    QStylePainter painter(this);
    painter.setPen(palette().color(QPalette::Text));

    // draw the combobox frame, focusrect and selected etc.
    QStyleOptionComboBox opt;
    initStyleOption(&opt);

    // if no display text been set , use "..." as default
    if(m_DisplayText.isNull())
        opt.currentText = "......";
    else
        opt.currentText = m_DisplayText;
    painter.drawComplexControl(QStyle::CC_ComboBox, opt);

    // draw the icon and text
    painter.drawControl(QStyle::CE_ComboBoxLabel, opt);

}


void CheckBoxList::SetDisplayText(QString text)
{
    m_DisplayText = text;

}

QString CheckBoxList::GetDisplayText() const
{
    return m_DisplayText;
}
#包括“CheckBoxList.h”
#包括
//内部私人代表
类CheckBoxListDelegate:公共QItemDelegate
{
公众:
CheckBoxListDelegate(QObject*父对象)
:QItemDelegate(父级)
{
;
}
无效油漆(QPainter*油漆工、const QstyleOption视图项目和选项、,
常数QModelIndex和索引)常数
{
//获取项目数据
bool value=index.data(Qt::UserRole.toBool();
QString text=index.data(Qt::DisplayRole).toString();
//使用项目数据填充样式选项
常量QStyle*style=QApplication::style();
QSTYLEOPTION按钮opt;
opt.state |=值?QStyle::state_打开:QStyle::state_关闭;
opt.state |=QStyle::state_已启用;
opt.text=文本;
opt.rect=option.rect;
//将项目数据绘制为复选框
样式->绘图控件(QStyle::CE_复选框,&opt,painter);
//QMessageBox::信息(0,“信息”,文本);
}
QWidget*createEditor(QWidget*parent,
常量QSTYLEOPTION视图项和选项,
常数QModelIndex和索引)常数
{
//创建复选框作为我们的编辑器
QCheckBox*编辑器=新的QCheckBox(父级);
返回编辑器;
}
void setEditorData(QWidget*编辑器,
常数QModelIndex和索引)常数
{
//设置编辑器数据
QCheckBox*myEditor=static_cast(编辑器);
myEditor->setText(index.data(Qt::DisplayRole.toString());
myEditor->setChecked(index.data(Qt::UserRole.toBool());
//
}
void setModelData(QWidget*编辑器,QBStractItemModel*模型,
常数QModelIndex和索引)常数
{
//从编辑器中获取值(复选框)
QCheckBox*myEditor=static_cast(编辑器);
bool value=myEditor->isChecked();
//设置模型数据
QMap数据;
插入(Qt::DisplayRole,myEditor->text());
data.insert(Qt::UserRole,value);
模型->设置项目数据(索引、数据);
}
void updateEditor几何体(QWidget*编辑器,
常量QStyleOptionViewItem&option,常量QModelIndex&index)常量
{
编辑器->设置几何体(option.rect);
}
};     
//最小宽度:10em;
CheckBoxList::CheckBoxList(QWidget*widget)
:QComboBox(小部件),m_显示文本(0)
{
//设置代理项目视图
view()->setItemDelegate(新的CheckBoxListDelegate(this));
//view()->setStyleSheet(“填充:15px;”);
//启用“项目”视图上的编辑
view()->setEditTriggers(QAbstractItemView::CurrentChanged);
//将“CheckBoxList::eventFilter”设置为项目视图的事件筛选器
view()->viewport()->installEventFilter(此);
//把它当作消遣是很酷的;)
view()->SetAlternatingRowColor(真);
}
CheckBoxList::~CheckBoxList()
{
;
}
bool CheckBoxList::eventFilter(QObject*对象,QEvent*事件)
{
//释放鼠标按钮后不要关闭项目视图
//通过简单地吃鼠标按钮在items视图的视口中释放
如果(事件->类型()==QEvent::MouseButtonRelease&&object==view()->viewport())
{
返回true;
}
返回QComboBox::eventFilter(对象、事件);
}
无效复选框列表::paintEvent(QPaintEvent*)
{
画家(本);
painter.setPen(调色板().color(qpalete::Text));
//绘制组合框框架、focusrect和selected等。
QSTYLEOPTIONCOMBOX opt;
initStyleOption(&opt);
//如果未设置显示文本,则使用“…”作为默认值
if(m_DisplayText.isNull())
opt.currentText=“……”;
其他的
opt.currentText=m_DisplayText;
painter.drawComplexControl(QStyle::CC_组合框,可选);
//绘制图标和文本
drawControl(QStyle::CE_ComboBoxLabel,可选);
}
无效复选框列表::SetDisplayText(QString文本)
{
m_DisplayText=文本;
}
QString复选框列表::GetDisplayText()常量
{
返回m_DisplayText;
}

如何知道选中了哪些复选框以便生成显示文本?当视图中只有一个复选框时,代码似乎有问题。它似乎忽略了鼠标的点击,并且没有检查。我假设它与组合框中的eventFilter有关。有任何修正吗?从
QComboBox
派生此类是一个错误,因为您无法正确覆盖不再相关的功能,因为所讨论的功能不是
虚拟的