其他分享
首页 > 其他分享> > QT乱翻书-可编辑下拉复选框

QT乱翻书-可编辑下拉复选框

作者:互联网

跳转到大纲

 

QT乱翻书-可编辑下拉复选框

#ifndef XIALAWIDGET_H
#define XIALAWIDGET_H

#include <QWidget>
#include<QListWidget>
#include<QLineEdit>

QT_BEGIN_NAMESPACE
namespace Ui
{
    class XiaLaWidget;
}
QT_END_NAMESPACE

class XiaLaWidget : public QWidget
{
    Q_OBJECT

public:
    XiaLaWidget(QWidget *parent = nullptr);
    ~XiaLaWidget();
private:
    QListWidget *pListWidget;
    QLineEdit *pLineEdit;
    //所有的选项
    QStringList selectText;
    //选中的选项
    QStringList selectedText;
    //处理选择操作信号
    void stateChanged(int state);

public slots:
    //接收传入数据 text1为所有选项,text2为选中的选项
    void revSelectedText(const QStringList& text1, const QStringList& text2);

signals:
    //将最终选中的选项发送出去.
    void sendSelectedText(const QStringList&);

private:
    Ui::XiaLaWidget *ui;
};
#endif // XIALAWIDGET_H
#include "xialawidget.h"
#include "ui_xialawidget.h"
#include<QtDebug>
#include<QCheckBox>
#include<QDebug>
#include<QComboBox>


XiaLaWidget::XiaLaWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::XiaLaWidget)
{
    ui->setupUi(this);
    //combox设置属性
    ui->comboBox->setInsertPolicy(QComboBox::InsertAtTop);
    //两个控件,用以combox替代
    pListWidget = new QListWidget(this);
    pLineEdit = new QLineEdit(this);
    //*如果设置可编辑 pLineEdit->setReadOnly(false);
    pLineEdit->setReadOnly(false);
    //设置给combox
    ui->comboBox->setModel(pListWidget->model());
    ui->comboBox->setView(pListWidget);
    ui->comboBox->setLineEdit(pLineEdit);
    //槽函数,处理QComboBox::textActivated. 加入新增选项并选中
    connect(ui->comboBox, QOverload<const QString &>::of(&QComboBox::textActivated),
    [ = ](const QString & text) {
        qDebug() << "[ = ](const QString & text) { " << text;
        //逗号分割,新增选项加入pListWidget
        if(text != "") {
            //新增项目已经加入到了pListWidget中,精准查找
            QList<QListWidgetItem*> lwis =  pListWidget->findItems(text, Qt::MatchExactly);
            qDebug() << "size" << lwis.size();
            QListWidgetItem* newText = lwis.at(0);
            QStringList sl = newText->text().replace(",", ",").trimmed().split(",");
            //新项目添加到了top位置,我们先删掉,然后重新分解加入pListWidget
            pListWidget->model()->removeRow(0);
            //for循环添加新的选项a,b,c,
            for(int ix = 0; ix < sl.length(); ix++) {
                QString tmp = sl.at(ix);
                if(tmp != "" && selectText.count(tmp) == 0) {
                    revSelectedText(QStringList(tmp), QStringList(tmp));
                }
            }
            stateChanged(Qt::Checked);
        }
    });
    //测试数据
    QStringList myList;
    myList.append("option01");
    myList.append("option02");
    myList.append("option03");
    myList.append("option04");
    myList.append("option05");
    myList.append("option06");
    myList.append("option07");
    revSelectedText(myList, QStringList());
}

XiaLaWidget::~XiaLaWidget()
{
    qDebug() << "XiaLaWidget::~XiaLaWidget()";
    delete ui;
}



void XiaLaWidget::stateChanged(int state)
{
    qDebug() << "void XiaLaWidget::stateChanged(int state) " << state;
    //state 选中是2,取消选中是0.
    QString lineText;//把选择的内容显示到pLineEdit.
    selectedText.clear();//把选择的内容记录到selectedText.
    int nCount = pListWidget->count();
    //for循环把选择的内容提取到两个变量里待用.为啥要用两个变量呢?因为任性.
    for (int i = 0; i < nCount; ++i) {
        QListWidgetItem *pItem = pListWidget->item(i);
        QWidget *pWidget = pListWidget->itemWidget(pItem);
        QCheckBox *pCheckBox = (QCheckBox *)pWidget;
        if (pCheckBox->isChecked()) {
            QString strText = pCheckBox->text();
            //用逗号隔开
            lineText.append(strText).append(",");
            selectedText.append(strText);
        }
    }
    if (!lineText.isEmpty()) {
        pLineEdit->setText(lineText);
        pLineEdit->setToolTip(lineText);
    } else {
        pLineEdit->clear();
        selectedText.clear();
    }
    qDebug() << "your choese nums:" << selectedText;
    //把选择的数据通过信号发送出去.
    emit sendSelectedText(selectedText);
}

void XiaLaWidget::revSelectedText(const QStringList& text1, const QStringList& text2)
{
    qDebug() << "XiaLaWidget::revSelectedText ";
    for(int ix = 0; ix < text1.length(); ix++) {
        QString six =  text1.at(ix);
        if(selectText.count(six) == 0) {
            QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
            QCheckBox *pCheckBox = new QCheckBox(pListWidget);
            pCheckBox->setText(six);
            pItem->setData(Qt::UserRole, pCheckBox->text());
            pListWidget->addItem(pItem);
            selectText.append(six);
            pListWidget->setItemWidget(pItem, pCheckBox);
            //当复选框状态发生变化时
            connect(pCheckBox, &QCheckBox::stateChanged, this, &XiaLaWidget::stateChanged);
            if(text2.count(six) > 0) {
                pCheckBox->setCheckState(Qt::Checked);
            }
        }
    }
}
/********************************************************************************
** Form generated from reading UI file 'xialawidget.ui'
**
** Created by: Qt User Interface Compiler version 5.15.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_XIALAWIDGET_H
#define UI_XIALAWIDGET_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_XiaLaWidget
{
public:
    QVBoxLayout *verticalLayout;
    QComboBox *comboBox;

    void setupUi(QWidget *XiaLaWidget)
    {
        if (XiaLaWidget->objectName().isEmpty())
            XiaLaWidget->setObjectName(QString::fromUtf8("XiaLaWidget"));
        XiaLaWidget->resize(193, 50);
        verticalLayout = new QVBoxLayout(XiaLaWidget);
        verticalLayout->setSpacing(0);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->setContentsMargins(0, 0, 0, 0);
        comboBox = new QComboBox(XiaLaWidget);
        comboBox->setObjectName(QString::fromUtf8("comboBox"));
        comboBox->setEditable(false);

        verticalLayout->addWidget(comboBox);


        retranslateUi(XiaLaWidget);

        QMetaObject::connectSlotsByName(XiaLaWidget);
    } // setupUi

    void retranslateUi(QWidget *XiaLaWidget)
    {
        XiaLaWidget->setWindowTitle(QCoreApplication::translate("XiaLaWidget", "XiaLaWidget", nullptr));
    } // retranslateUi

};

namespace Ui {
    class XiaLaWidget: public Ui_XiaLaWidget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_XIALAWIDGET_H
#include "xialawidget.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    XiaLaWidget w;
    w.show();
    return a.exec();
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>XiaLaWidget</class>
 <widget class="QWidget" name="XiaLaWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>193</width>
    <height>50</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>XiaLaWidget</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <property name="spacing">
    <number>0</number>
   </property>
   <property name="leftMargin">
    <number>0</number>
   </property>
   <property name="topMargin">
    <number>0</number>
   </property>
   <property name="rightMargin">
    <number>0</number>
   </property>
   <property name="bottomMargin">
    <number>0</number>
   </property>
   <item>
    <widget class="QComboBox" name="comboBox">
     <property name="editable">
      <bool>false</bool>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

 

标签:XiaLaWidget,QT,pListWidget,comboBox,复选框,ui,翻书,include,append
来源: https://www.cnblogs.com/kanbuxiaqu/p/15792152.html