【Qt】QListWidget 添加控件
每天一个小知识:设置QListWidget的显示模式
设置显示模式
QListView::ListMode
QListView::IconMode
调用接口:
setViewMode(QListWidget::IconMode); //显示模式
添加控件
效果
效果
新建项目的时候创建一个带ui的项目,新建完成后,在界面拖拽一个QListWidget。
Widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QListWidget>
#include <QStringList>
#include <QString>
#include <QDebug>
#include <QListWidgetItem>
#include <QGridLayout>
#include <QToolButton>
#include <QLineEdit>
#include <QRadioButton>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
void initListWidget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
initListWidget();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initListWidget()
{
ui->listWidget->setCurrentRow(2);
QStringList StrListHeader;
StrListHeader << QString("选中") << QString("序号") << QString("文件名");
new QListWidgetItem(tr("Oak"), ui->listWidget);
new QListWidgetItem(tr("Fir"), ui->listWidget);
new QListWidgetItem(tr("Pine"), ui->listWidget);
QListWidgetItem *ListItem = new QListWidgetItem;
// ListItem->setText();
QListWidgetItem *WContainerItem = new QListWidgetItem(ui->listWidget);
QWidget *WContainer = new QWidget(ui->listWidget);//新建一个QWidget窗口
QGridLayout*Glayout = new QGridLayout;
/*************start*****************/
/* 在这里添加需要的控件 */
QLineEdit *lineEdit = new QLineEdit;
QLineEdit *lineEdit_1 = new QLineEdit;
QToolButton *toolButton = new QToolButton;
QRadioButton *radioButton = new QRadioButton;
QRadioButton *radioButton_1 = new QRadioButton;
Glayout->addWidget(lineEdit,0,0);
Glayout->addWidget(radioButton,0,1);
Glayout->addWidget(lineEdit_1,0,2);
Glayout->addWidget(radioButton_1,0,3);
WContainer->setLayout(Glayout);
/******************end****************/
WContainerItem->setSizeHint(QSize(40,40));
ui->listWidget->setItemWidget(WContainerItem, WContainer);//将WContainer赋予WContainerItem
}
原文:https://blog.csdn.net/Fuel_Ming/article/details/121792409