程序源码如下:
mylistwidgetitem.h文件:
#ifndef MYLISTWIDGETITEM_H
#define MYLISTWIDGETITEM_H
#include <QWidget>
#include <QCheckBox>
#include <QRadioButton>
#include <QListWidget>
#include <QListWidgetItem>
#include <QToolButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class MyListWidgetItem : public QWidget
{ Q_OBJECTpublic:
//explicit //MyListWidgetItem(QString regionAddressArg,QList<QString> meterAddressListArg,QWidget *parent = 0);
MyListWidgetItem(QString regionAddressArg,QList<QString> meterAddressListArg,QWidget *parent);private:
void initialMainWindow();
QString regionAddress;
QList<QString> meterAddressList;
signals:public slots:
};#endif // MYLISTWIDGETITEM_H
mylistwidgetitem.cpp文件
#include "mylistwidgetitem.h"MyListWidgetItem::MyListWidgetItem(QString regionAddressArg,QList<QString> meterAddressListArg,QWidget *parent) : QWidget(parent){
regionAddress = regionAddressArg;
meterAddressList = meterAddressListArg;
initialMainWindow();
}
void MyListWidgetItem::initialMainWindow(){
QCheckBox *regionCheckBox = new QCheckBox(regionAddress,this);
regionCheckBox->setFixedSize(100,20);
QRadioButton *allSelectRadio = new QRadioButton(tr("全选"),this);
allSelectRadio->setFixedSize(60,20);
QRadioButton *againSelectRadio = new QRadioButton(tr("反选"),this);
againSelectRadio->setFixedSize(60,20);
QToolButton *headToolButton = new QToolButton(this);
headToolButton->setText(tr("隐藏"));
headToolButton->setFixedSize(50,20);
QHBoxLayout *headLayout = new QHBoxLayout;
headLayout->setMargin(0);
headLayout->setSpacing(0);
headLayout->addWidget(regionCheckBox);
headLayout->addWidget(allSelectRadio);
headLayout->addWidget(againSelectRadio);
headLayout->addStretch();
headLayout->addWidget(headToolButton);
QListWidget *meterAddListWidget = new QListWidget;
meterAddListWidget->setViewMode(QListView::IconMode);
meterAddListWidget->setResizeMode(QListView::Adjust);
for(int i = 0; i < 30; i++){
QListWidgetItem *item = new QListWidgetItem("112233445566");
item->setCheckState(Qt::Unchecked);
meterAddListWidget->addItem(item);
}
//添加主布局
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);
mainLayout->setSpacing(2);
mainLayout->addLayout(headLayout);
mainLayout->addWidget(meterAddListWidget);
this->setLayout(mainLayout);
}
mylistwidget.h文件:
#ifndef MYLISTWIDGET_H#define MYLISTWIDGET_H#include <QWidget>#include "mylistwidgetitem.h"class MyListWidget : public QWidget{
Q_OBJECTpublic:
//explicit
//MyListWidget(QWidget *parent = 0);
MyListWidget(QWidget *parent);
~MyListWidget();
private:
QString *regionAddress;
QList<QString> meterAddressList1;
QList<QString> meterAddressList2;signals:public slots:
};#endif // MYLISTWIDGET_H
mylistwidget.cpp文件:
#include "mylistwidget.h"#include "mylistwidgetitem.h"#include <QVBoxLayout>#include <QDebug>MyListWidget::MyListWidget(QWidget *parent) : QWidget(parent){
meterAddressList1 << "112233445566" << "112233445566";
meterAddressList2 << "112233445566" << "112233445566";
qDebug() << "hello";
MyListWidgetItem *mylistWidgetItem1 = new MyListWidgetItem("112233445566",meterAddressList1,this);
MyListWidgetItem *mylistWidgetItem2 = new MyListWidgetItem("112233445577",meterAddressList2,this);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);