Home > OS >  Consult a multi-field sorting problem in QT.
Consult a multi-field sorting problem in QT.

Time:11-12

Common. H

 
# # ifndef COMMON_H
# define COMMON_H

#include

Struct StatusCode {
QString EN;
QString order;
QString CN;

StatusCode () {}

StatusCode (const StatusCode& A) : EN a.E (N), the order (a.o rder), CN (a.C N) {}
};

# endif//COMMON_H



Widget. H

 
# # ifndef WIDGET_H
# define WIDGET_H

#include
#include
#include
# include "is common. H"
#include
#include

The namespace Ui {
The class Widget;
}

The class Widget: public QWidget
{
Q_OBJECT

Public:
Explicit Widget (QWidget * parent=nullptr);
~ the Widget ();

Void getData ();
Void setData ();
QListBool static stdSort (StatusCode * StatusCode1, StatusCode * StatusCode2);

Private slots:
Void on_pushButton_clicked ();

Private:
Ui: Ui widgets *;
QStandardItemModel * model_tableView;
};

# endif//WIDGET_H





Widget. The CPP

 
# include "widget. H"
# include "ui_widget. H"

# include "QMessageBox
"# include "QDebug
"#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


Widgets: : widgets (QWidget * parent) :
QWidget (parent),
UI (new UI: : Widget)
{
The UI - & gt; SetupUi (this);

Model_tableView=new QStandardItemModel (this);

}

Widgets: : ~ Widget ()
{
Delete the UI;
}


Void Widget: : setData ()
{
StatusCode * entity=new StatusCode ();

The entity - & gt; EN="BBC";
The entity - & gt; The order="1";
The entity - & gt; In the CN="";
StatusCode_List. Append (entity);

The entity=nullptr;
The entity=new StatusCode ();

The entity - & gt; EN="BBC";
The entity - & gt; The order="1";
The entity - & gt; CN="English";
StatusCode_List. Append (entity);

The entity=nullptr;
The entity=new StatusCode ();

The entity - & gt; EN="ABC".
The entity - & gt; The order="3";
The entity - & gt; In the CN="1";
StatusCode_List. Append (entity);

The entity=nullptr;
The entity=new StatusCode ();

The entity - & gt; EN="ABC".
The entity - & gt; The order="2";
The entity - & gt; In the CN="2";
StatusCode_List. Append (entity);

STD: : sort (StatusCode_List. The begin (), StatusCode_List. The end (), stdSort);

The entity=nullptr;
}



Void Widget: : getData ()
{
Model_tableView - & gt; SetHorizontalHeaderItem (0, new QStandardItem (" English "));
Model_tableView - & gt; SetHorizontalHeaderItem (1, new QStandardItem (" digital "));
Model_tableView - & gt; SetHorizontalHeaderItem (2, new QStandardItem (" Chinese "));

for(int i=0; i{
Int k=0;
Model_tableView - & gt; SetItem (I, k, new QStandardItem (StatusCode_List [I] - & gt; EN));
k++;
Model_tableView - & gt; SetItem (I, k, new QStandardItem (StatusCode_List [I] - & gt; The order));
k++;
Model_tableView - & gt; SetItem (I, k, new QStandardItem (StatusCode_List [I] - & gt; CN));
k++;
}

The UI - & gt; TableView - & gt; SetModel (model_tableView);
}



Void Widget: : on_pushButton_clicked ()
{
SetData ();
GetData ();
}

Bool Widget: : stdSort (StatusCode1 StatusCode * and StatusCode * StatusCode2)
{
If (QString: : compare (StatusCode1 - & gt; EN, StatusCode2 - & gt; EN) & lt; 0)
{
return true;
} else if (QString: : compare (StatusCode1 - & gt; EN, StatusCode2 - & gt; EN) & gt; 0)
{
Return false;
}

If (QString: : compare (StatusCode1 - & gt; Order, StatusCode2 - & gt; The order) & lt; 0)
{
return true;
} else if (QString: : compare (StatusCode1 - & gt; Order, StatusCode2 - & gt; The order) & gt; 0)
{
Return false;
}

QLocale loc (QLocale: : Chinese, QLocale: : China);
QCollator qcol (loc);
QLocale cn (QLocale: : Chinese);
QCollator cr (cn);
Int nResult=0;

Char * ch1;
QByteArray ba1=StatusCode1 - & gt; CN. ToLatin1 ();
Ch1=ba1. Data ();
QString str1;
Str1=QString: : fromLocal8Bit (ch1);

Char * ch2;
QByteArray ba2=StatusCode2 - & gt; CN. ToLatin1 ();
Ch2=ba2. Data ();
QString str2;
Str2=QString: : fromLocal8Bit (ch2);

Pare said nResult=cr.com (str1, str2);

If (nResult<0)
{
return true;
} else if (nResult> 0)
{
Return false;
}
}




The original order is:

The BBC 1
The BBC English 1
ABC 3 in 1
ABC 2 2

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related