QListWidget
1.基本属性
2.插入和或其选中的内容
3.遍历和排序
4.插入其他控件和编辑
QListWidget属性
1.selectionMode 单选多选
2.flow从上到下还是从左到右
3.gridSize每一行的宽高
4.iconSize 图标的宽高
QListWidget插入数据
QListWidgetItem *newItem = new QListWidgetItem;
newItem->setText(itemText);
listWidget->insertItem(row,newItem);
new QListWidgetItem("Hazel",listWidget);
void addItem(const QString &label);
void QListWidget::addItems(const QStringList &labels);
[slot] void QListWidget::clear();
QListWidget setEditTriggers
listWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
item->setFlags(Qt::ItemlsSelectable|Qt::ItemlsEnabled|Qt::ItemlsEditable);
NoEditTriggers 0 No editing possible
CurrentChanged 1 Editing start whenever current item changes
DoubleClicked 2 Editing starts when an item is double clicked
SelectedClicked 4 Editing starts when clicking on an already selected item
EditKeyPressed 8 Editing starts when the platform edit key has been pressed over an item
AnyKeyPressed 16 Editing starts when any key is pressed over an item
AllEditTriggers 31 Editing starts for all above actions
QListWidget 遍历
for(int i = 0;i < ui.listWidget->count();i++)
{
qDebug()<<ui.listWidget->item(i)->text();
}
QListWidget信号事件
void currentItemChanged(QListWidgetItem *current,QListWidgetItem*previous) 鼠标选中
void currentRowChanged(int currentRow) 鼠标键盘选中
void currentTextChanged(const QString ¤tText) 鼠标键盘选中
void itemActivated(QListWidgetItem *item) 激活 双击
void itemChanged(QListWidgetItem *item) 列表内容改变
void itemDoubleClicked(QListWidgetItem *item) 鼠标左右健
void itemEntered(QListWidgetItem *item) setMouseTracking(true); 鼠标移上去
void itemPressed(QListWidgetItem *item) 鼠标左右健按下
void itemClicked(QListWidgetItem *item) 鼠标左键松开
void itemSelectionChanged()
ui_widget.cpp
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
#include <XSlider.h>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
XSlider *horizontalSlider;
QPushButton *pushButton;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
horizontalSlider = new XSlider(Widget);
horizontalSlider->setObjectName(QString::fromUtf8("horizontalSlider"));
horizontalSlider->setGeometry(QRect(140, 220, 461, 21));
horizontalSlider->setStyleSheet(QString::fromUtf8("QSlider::groove{\n"
" background-color: rgba(200, 74, 254,200);\n"
" height: 10px;\n"
" border:3px rgba(200, 74, 254,200);\n"
" border-radius:10px;\n"
"}\n"
"\n"
"QSlider::handle{\n"
" width:30px;\n"
" background-color: rgba(215, 10, 0,200);\n"
" border-radius:10px;\n"
" margin:-10px; \n"
"}\n"
"\n"
"QSlider::sub-page{\n"
" background-color: rgba(125, 53, 102,140);\n"
"}\n"
"\n"
"QSlider::add-page{\n"
" background-color: rgba(25, 53, 102,140);\n"
"}\n"
""));
horizontalSlider->setMaximum(999);
horizontalSlider->setSingleStep(100);
horizontalSlider->setPageStep(500);
horizontalSlider->setValue(100);
horizontalSlider->setTracking(true);
horizontalSlider->setOrientation(Qt::Horizontal);
horizontalSlider->setInvertedAppearance(false);
horizontalSlider->setInvertedControls(false);
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(80, 60, 89, 25));
retranslateUi(Widget);
QObject::connect(horizontalSlider, SIGNAL(sliderMoved(int)), Widget, SLOT(Move(int)));
QObject::connect(horizontalSlider, SIGNAL(valueChanged(int)), Widget, SLOT(Change(int)));
QObject::connect(horizontalSlider, SIGNAL(sliderPressed()), Widget, SLOT(Press()));
QObject::connect(horizontalSlider, SIGNAL(sliderReleased()), Widget, SLOT(Rel()));
QObject::connect(pushButton, SIGNAL(clicked()), Widget, SLOT(Click()));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
pushButton->setText(QCoreApplication::translate("Widget", "PushButton", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->listWidget->clear();
QListWidgetItem *item1 = new QListWidgetItem;
item1->setText(QString::fromLocal8Bit("测试数据1"));
ui->listWidget->insertItem(0,item1);
new QListWidgetItem("test item2 ",ui->listWidget);
ui->listWidget->addItem("test item 3");
QListWidgetItem *itemIcon = new QListWidgetItem;
itemIcon->setText("test item icon");
itemIcon->setIcon(QIcon("test.jpg"));
ui->listWidget->addItem(itemIcon);
//遍历列表
ui->listWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
for(int i = 0;i < ui->listWidget->count();i++)
{
qDebug()<<ui->listWidget->item(i)->text();
}
ui->listWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
for(int i = 0; i < ui->listWidget->count(); i++)
{
ui->listWidget->item(i)->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
//鼠标跟踪
ui->listWidget->setMouseTracking(true);
}
void Widget::Click()
{
for(int i = 0;i < ui->listWidget->count();i++)
{
qDebug()<<ui->listWidget->item(i)->text();
}
}
void Widget::Sort()
{
}
void Widget::CurChange(QListWidgetItem *cur,QListWidgetItem *pre)
{
qDebug()<<"CurChange QListWidgetItem "<<cur->text();
if(pre)
{
qDebug()<<pre->text();
}
}
void Widget::CurChange(int row)
{
qDebug()<<"CurChange row "<<row;
}
void Widget::CurChange(const QString &txt)
{
qDebug()<<"CurChange QString "<<txt;
}
//激活 左键双击
void Widget::ItemAct(QListWidgetItem *item)
{
qDebug()<<"ItemAct "<<item->text();
}
//内容改变 焦点移开 ,回车确认
void Widget::ItemChange(QListWidgetItem *item)
{
qDebug()<<"ItemChange "<<item->text();
}
//鼠标事件
void Widget::ItemClick(QListWidgetItem *item)
{
qDebug()<<"Item Click "<<item->text();
}
void Widget::ItemDoubleClick(QListWidgetItem *item)
{
qDebug()<<"Item double click "<<item->text();
}
void Widget::ItemEntered(QListWidgetItem *item)
{
qDebug()<<"Item Entered "<<item->text();
}
void Widget::ItemPressed(QListWidgetItem *item)
{
qDebug()<<"Item Pressed "<<item->text();
}
Widget::~Widget()
{
delete ui;
}
在列表中显示其他Widget控件
QLineEdit *e = new QLineEdit("Test Line");
QSpinBox *s = new QSpinBox();
ui.listWidget->setItemWidget(ui.listWidget->item(2),e);
ui.listWidget->setItemWidget(ui.listWidget->item(3),s);
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QLineEdit>
#include <QSpinBox>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
ui->listWidget->clear();
QListWidgetItem *item1 = new QListWidgetItem;
item1->setText(QString::fromLocal8Bit("测试数据1"));
ui->listWidget->insertItem(0,item1);
new QListWidgetItem("test item2 ",ui->listWidget);
ui->listWidget->addItem("test item 3");
QListWidgetItem *itemIcon = new QListWidgetItem;
itemIcon->setText("test item icon");
itemIcon->setIcon(QIcon("test.jpg"));
ui->listWidget->addItem(itemIcon);
//遍历列表
ui->listWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
for(int i = 0;i < ui->listWidget->count();i++)
{
qDebug()<<ui->listWidget->item(i)->text();
}
ui->listWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
for(int i = 0; i < ui->listWidget->count(); i++)
{
ui->listWidget->item(i)->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
//鼠标跟踪
ui->listWidget->setMouseTracking(true);
//显示QLineEdit控件
QLineEdit *e = new QLineEdit("Test Line Edit");
ui->listWidget->setItemWidget(ui->listWidget->item(2),e);
//显示QSpinBox
QSpinBox *s = new QSpinBox();
ui->listWidget->setItemWidget(ui->listWidget->item(3),s);
}
void Widget::Click()
{
for(int i = 0;i < ui->listWidget->count();i++)
{
qDebug()<<ui->listWidget->item(i)->text();
}
}
void Widget::Sort()
{
}
void Widget::CurChange(QListWidgetItem *cur,QListWidgetItem *pre)
{
qDebug()<<"CurChange QListWidgetItem "<<cur->text();
if(pre)
{
qDebug()<<pre->text();
}
}
void Widget::CurChange(int row)
{
qDebug()<<"CurChange row "<<row;
}
void Widget::CurChange(const QString &txt)
{
qDebug()<<"CurChange QString "<<txt;
}
//激活 左键双击
void Widget::ItemAct(QListWidgetItem *item)
{
qDebug()<<"ItemAct "<<item->text();
}
//内容改变 焦点移开 ,回车确认
void Widget::ItemChange(QListWidgetItem *item)
{
qDebug()<<"ItemChange "<<item->text();
}
//鼠标事件
void Widget::ItemClick(QListWidgetItem *item)
{
qDebug()<<"Item Click "<<item->text();
}
void Widget::ItemDoubleClick(QListWidgetItem *item)
{
qDebug()<<"Item double click "<<item->text();
}
void Widget::ItemEntered(QListWidgetItem *item)
{
qDebug()<<"Item Entered "<<item->text();
}
void Widget::ItemPressed(QListWidgetItem *item)
{
qDebug()<<"Item Pressed "<<item->text();
}
Widget::~Widget()
{
delete ui;
}