之前因为工作需要,将下拉框comboBox改成了弹出表格的形式。
先看一下效果:
弹出框样式:
点击之后,会将indexItem中的字符串传至下拉框中:
头文件:
#ifndef MYCOMBOBOX_H
#define MYCOMBOBOX_H
#include <QObject>
#include <QComboBox>
#include <QModelIndex>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QTableView>
#include <QMouseEvent>
#include <QVBoxLayout>
class MyCombobox : public QComboBox
{
Q_OBJECT
public:
explicit MyCombobox(QWidget *parent=0);
~MyCombobox()override;
void insertItems(QStringList &textList);
void setItemWidth(int width);
void setItemHeight(int Height);
void setTablePara(QStringList &textList);
public slots:
void ReceiveIndex();
protected:
void showPopup()override;
void mousePressEvent(QMouseEvent *e)override;
private:
QVBoxLayout *layout;
QDialog *popup;
QTableView *View;
QStandardItemModel *pModel;
QList<QStandardItem *> List_Item;
QStandardItem *Item;
QStringList list;
int col_Rest;
int row_Rest;
int customizeRow=5;
private:
void insertItem(int row, int col, const QString & text);
signals:
void clicked();
void ChangedFromTable();
};
#endif // MYCOMBOBOX_H
源文件:
#include "mycombobox.h"
#include <QTableView>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QDebug>
#include <QHeaderView>
MyCombobox::MyCombobox(QWidget *parent):QComboBox(parent)
{
setStyleSheet("QComboBox { min-height: 80px; min-width: 160px; }");
this->setEditable(true);
View=new QTableView();
this->setView(View);
View->setHidden(false);
View->verticalHeader()->setDefaultSectionSize(80);
View->horizontalHeader()->setDefaultSectionSize(90);
View->horizontalHeader()->setVisible(false);
View->verticalHeader()->setVisible(false);
this->connect(this,&MyCombobox::clicked,this,&MyCombobox::showPopup);
connect(View,SIGNAL(clicked(QModelIndex)),this,SLOT(ReceiveIndex()));
layout =new QVBoxLayout();
layout->addWidget(View);
}
MyCombobox::~MyCombobox()
{
}
void MyCombobox::insertItem(int row, int col, const QString &text)
{
Item=new QStandardItem(QString(text).arg(row).arg(col));
Item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
pModel->setItem(row,col,Item);
}
//把信息塞进表格
void MyCombobox::insertItems(QStringList &textList)
{
list.clear();
list=textList;
setTablePara(textList);
int row,col;
for(row=0;row<row_Rest;row++)
{
//行数有余数的情况
if(textList.length()>customizeRow&&textList.length()%customizeRow>0)
{
//行数不是最后一行
if(row<row_Rest-1)
{
for(col=0;col<customizeRow;col++)
{
int index=row*customizeRow+col;
QString text=textList.at(index);
this->insertItem(row,col,text);
}
}else if(row=row_Rest-1)
{
//最后一行
for(col=0;col<col_Rest;col++)
{
int index=row*customizeRow+col;
QString text=textList.at(index);
this->insertItem(row,col,text);
}
}
}else if(textList.length()>customizeRow&&textList.length()%customizeRow==0)
{
if(row<row_Rest)
{
for(col=0;col<customizeRow;col++)
{
int index=row*customizeRow+col;
QString text=textList.at(index);
this->insertItem(row,col,text);
}
}
}else if(textList.length()<customizeRow)
{
for(col=0;col<textList.length();col++)
{
int index=row*customizeRow+col;
QString text=textList.at(index);
this->insertItem(row,col,text);
}
}
}
}
void MyCombobox::setTablePara(QStringList &textList)
{
row_Rest=textList.length()/customizeRow;
col_Rest;
if(textList.length()>customizeRow&&textList.length()%customizeRow==0)
{
pModel = new QStandardItemModel(row_Rest+1,customizeRow);
}else if(textList.length()>customizeRow&&textList.length()%customizeRow>0)
{
row_Rest=row_Rest+1;
col_Rest=textList.length()%customizeRow;
pModel = new QStandardItemModel(row_Rest,customizeRow);
}else if(textList.length()<customizeRow)
{
row_Rest=1;
pModel = new QStandardItemModel(row_Rest,customizeRow);
}
this->view()->setFixedHeight(row_Rest*80);
this->view()->setFixedWidth(customizeRow*95);
View->setModel(pModel);
}
void MyCombobox::ReceiveIndex()
{
int index=View->currentIndex().row()*customizeRow+View->currentIndex().column();
if(index>list.length()-1)
{
this->setCurrentText("");
popup->close();
return;
}
this->setCurrentText(list.at(index));
emit ChangedFromTable();
popup->close();
}
void MyCombobox::showPopup()
{
//showpopup重载
popup = new QDialog();
popup->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
popup->setLayout(layout);
if(row_Rest<8)
{
popup->setMinimumHeight(row_Rest*80);
}else{
popup->setMinimumHeight(560);
}
popup->setFixedWidth(customizeRow*95);
popup->exec();
}
void MyCombobox::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
emit clicked(); //触发clicked信号
}
}
因为是继承的comboBox,所以只需要将QcomboBox提升为mycombobox即可