#include "ExpertListWidget.h" #include #include #include #include #include #include ExpertListWidget::ExpertListWidget(QWidget *parent) : QWidget(parent) { initialize(); initLayout(); connectSignalsAndSlots(); } void ExpertListWidget::showUsers(const QList &list) { m_listWidget->clear(); for (int i = 0; i < list.count(); i++) { QListWidgetItem *item = new QListWidgetItem; item->setSizeHint(QSize(200, 60)); m_listWidget->addItem(item); QWidget *w = new QWidget(); m_listWidget->setItemWidget(item, w); QHBoxLayout *hBox = new QHBoxLayout(w); hBox->setSpacing(0); hBox->setMargin(10); QLabel *idx = new QLabel(QString::number(i + 1)); idx->setFixedWidth(20); hBox->addWidget(idx); hBox->addSpacing(10); QLabel *name = new QLabel(list[i]->userName); hBox->addWidget(name); hBox->addStretch(); QLabel *time = new QLabel(list[i]->writeTime); hBox->addWidget(time); } } bool ExpertListWidget::hasSelectedItem() const { return m_listWidget->selectedItems().count() > 0; } int ExpertListWidget::selectedRow() const { QListWidgetItem *item = m_listWidget->selectedItems().first(); return m_listWidget->row(item); } void ExpertListWidget::clearSelection() { m_listWidget->clearSelection(); } void ExpertListWidget::initialize() { m_vBoxLayout = new QVBoxLayout(this); m_listWidget = new QListWidget(this); m_listWidget->setAlternatingRowColors(true); m_listWidget->setStyleSheet("QListWidget {border: 1px solid rgba(0, 0, 0, 0.073);background: rgb(255, 255, " "255);alternate-background-color: rgb(244, 244, 255);}"); } void ExpertListWidget::initLayout() { m_vBoxLayout->addWidget(m_listWidget); } void ExpertListWidget::connectSignalsAndSlots() { connect(m_listWidget, &QListWidget::itemDoubleClicked, this, &ExpertListWidget::slotItemDoubleClicked); connect(m_listWidget, &QListWidget::itemClicked, this, &ExpertListWidget::slotItemClicked); connect(m_listWidget, &QListWidget::currentItemChanged, this, &ExpertListWidget::slotCurrentItemChanged); connect(m_listWidget, &QListWidget::currentRowChanged, this, &ExpertListWidget::slotCurrentRowChanged); connect(m_listWidget, &QListWidget::itemSelectionChanged, this, &ExpertListWidget::slotItemSelectionChanged); } void ExpertListWidget::slotItemDoubleClicked(QListWidgetItem *item) { emit siganlItemDoubleClicked(m_listWidget->row(item)); } void ExpertListWidget::slotItemClicked(QListWidgetItem *) { qDebug() << __FUNCTION__ << __LINE__; } void ExpertListWidget::slotCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) { qDebug() << __FUNCTION__ << __LINE__ << current << previous; } void ExpertListWidget::slotCurrentRowChanged(int currentRow) { qDebug() << __FUNCTION__ << __LINE__ << currentRow; } void ExpertListWidget::slotItemSelectionChanged() { qDebug() << __FUNCTION__ << __LINE__ << m_listWidget->selectedItems(); emit signalSelectionChanged(); }