Qt 组合框QComboBox的定制

谁说胖子不能爱 提交于 2019-12-06 03:36:36

转载:https://www.cnblogs.com/csuftzzk/p/qss_combobox.html

转载:https://www.bbsmax.com/A/E35pLgRK5v/

转载:https://www.cnblogs.com/peterliang/p/3618820.html(QSplitter 拆分窗口)

转载:http://blog.sina.com.cn/s/blog_a6fb6cc90101i8it.html

Demo参考网上的例子,暂时记录下来,随后再整理

1.自定义combobox中的item控件

#include <QWidget>
#include <QLabel>

class ComboboxItem : public QWidget
{
    Q_OBJECT

public:
    ComboboxItem(QWidget *parent);
    ~ComboboxItem();

    void setLabelContent(const QString & str);

signals:
    void chooseAccount(const QString&);

private:
    QLabel* m_img;
    QLabel* m_label;
};

 

#include "ComboboxItem.h"
#include <QHBoxLayout>

ComboboxItem::ComboboxItem(QWidget *parent)
    : QWidget(parent)
{
    m_img = new QLabel(this);
    m_label = new QLabel(this);
    m_img->setStyleSheet("QLabel{background: rgb(255, 0, 0)}");
    m_img->setFixedSize(15, 20);

    QHBoxLayout* layout = new QHBoxLayout(this);

    layout->addWidget(m_img);
    layout->addWidget(m_label);
    layout->setContentsMargins(5, 5, 5, 5);

    setLayout(layout);
}

ComboboxItem::~ComboboxItem()
{
}

void ComboboxItem::setLabelContent(const QString & str)
{
    m_label->setText(str);
}

2.自定义代理

#include <QStyledItemDelegate>

class NoFocusFrameDelegate :public QStyledItemDelegate
{
    Q_OBJECT
public:
    NoFocusFrameDelegate(QObject* parent = 0);
    ~NoFocusFrameDelegate();


    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#include "NoFocusFrameDelegate.h"

NoFocusFrameDelegate::NoFocusFrameDelegate(QObject* parent /*= 0*/)
{

}

NoFocusFrameDelegate::~NoFocusFrameDelegate()
{

}

void NoFocusFrameDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem  view_option(option);
    if (view_option.state & QStyle::State_HasFocus) {
        view_option.state = view_option.state ^ QStyle::State_HasFocus;
    }

    QStyledItemDelegate::paint(painter, view_option, index);
}

 

3.主窗口中使用

#include <QtWidgets/QMainWindow>
#include "ui_QMyCombobox.h"

class QListWidget;

class QMyCombobox : public QMainWindow
{
    Q_OBJECT

public:
    QMyCombobox(QWidget *parent = Q_NULLPTR);

public slots:

   void onChooseAccount(const QString& str);



private:
    Ui::QMyComboboxClass ui;

    QListWidget* m_listWidget;
};
#include "QMyCombobox.h"
#include <QListWidget>
#include "NoFocusFrameDelegate.h"
#include "ComboboxItem.h"

QMyCombobox::QMyCombobox(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    m_listWidget = new QListWidget(this);

    // 设置子项目代理,否则下拉框选项周围会出现虚线框
    m_listWidget->setItemDelegate(new NoFocusFrameDelegate(this));
    ui.comboBox->setEditable(true);
    ui.comboBox->setModel(m_listWidget->model());
    ui.comboBox->setView(m_listWidget);

    // 在下拉框中添加5个选项
    for (int i = 0; i < 5; ++i)
    {
        ComboboxItem* item = new ComboboxItem(this);
        item->setLabelContent(QString("Account") + QString::number(i, 10));
        connect(item, SIGNAL(chooseAccount(const QString&)), this, SLOT(onChooseAccount(const QString&)));
        QListWidgetItem* widgetItem = new QListWidgetItem(m_listWidget);
        m_listWidget->setItemWidget(widgetItem, item);
    }
}

void QMyCombobox::onChooseAccount(const QString& str)
{
    ui.comboBox->setCurrentText(str);
}

 

最后效果:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!