简体   繁体   English

快速搜索QTableWidget行

[英]Fast search through QTableWidget rows

I need to search rows through a QTableWidget . 我需要通过QTableWidget搜索行。 Each of the rows in the table contains a field with date and I need to show only rows that are within a specified date interval based on user input. 表格中的每一行都包含一个带有日期的字段,我需要根据用户输入仅显示指定日期间隔内的行。 Here is my function: 这是我的功能:

void nvr::sort()
{

QTableWidget* tabela = this->findChild<QTableWidget*>("NCtable");



QDateEdit* c1 = this->findChild<QDateEdit*>("c1");

QDateEdit* c2 = this->findChild<QDateEdit*>("c2");

// user specified ranges for date
QDate date1 = c1->date();

QDate date2 = c2->date();

//row numbers in table
int rowsNum = tabela->rowCount();

// hide all rows
for(int z = 0; z < rowsNum; z++) {

    tabela->hideRow(z);

}

// show only rows that are within range
for(int z = 0; z < rowsNum; z++) {


    QDateTime dateTime = QDateTime::fromString(tabela->item(z,2)->text(),"dd.MM.yyyy hh:mm");

    QDate date = dateTime.date();

    //date compares
    if ( (date1.operator <=(date)) && (date2.operator >=(date) ) ) {

    tabela->showRow(z);

    }



   }



}

This works fine if i have 200 rows. 如果我有200行,这工作正常。 But when i have 30 000 rows and i surely will, the gui freezes because i suppose the function executes very slow. 但是当我有3万行并且我肯定会的时候,gui会冻结,因为我认为该函数的执行速度非常慢。 Any suggestions for faster execution? 有什么建议可以加快执行速度吗?

It is hard to reproduce your problem, but here is the approach I would take: 重现您的问题很困难,但是这是我要采取的方法:

  • Create a custom class to store the data of one row, let's call it DataRow . 创建一个自定义类来存储一行的数据,我们将其称为DataRow

  • Store those in a QVector<DataRow> , that you can sort by Date1 for example. 将它们存储在QVector<DataRow> ,例如,您可以按Date1对其进行排序。

  • Loop through this QVector<DataRow> and find the elements that correspond to the criteria. 遍历此QVector<DataRow>并找到与条件对应的元素。
  • Add those DataRow to a class derived from QAbstractItemModel . 将那些DataRow添加到派生自QAbstractItemModel的类中。
  • Show this model derived from QAbstractItemModel with a QTableView . 使用QTableView显示从QAbstractItemModel派生的模型。

QTableWidget is heavyweight and was not really built for speed. QTableWidget是重量级的,并不是真正为速度而构建的。 It's really convenient to build something quickly with few elements though. 不过,使用很少的元素快速构建内容确实很方便。 QTableView is the one you want, with a custom model inherited from QAbstractItemModel . QTableView是您想要的一个,它具有从QAbstractItemModel继承的自定义模型。

Then, when the user requests a new input, you could just wipe the model and restart the process. 然后,当用户请求新输入时,您只需擦除模型并重新启动过程即可。 This is not optimal, but the user should not see the difference. 这不是最佳选择,但用户看不到区别。 Feel free to add more logic here to keep the good elements and only remove the bad ones. 随时在此处添加更多逻辑以保留好元素,只删除坏元素。

About the GUI freezing, one way to always avoid that is to have the GUI thread separated from other worker threads. 关于GUI冻结,始终避免这种情况的一种方法是将GUI线程与其他工作线程分开。 The QThread documentation is exhaustive and can help you set up something like this. QThread文档非常详尽,可以帮助您设置类似的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM