QTreeWidget单击后缓慢删除

时间:2018-12-07 06:31:13

标签: c++ qt qtreeview qtreewidget qtreewidgetitem

我制作了一个QTreeWidget来显示非常大且连续的数据集。由于数据集是连续的,因此当总行数大于指定数量时,我将删除初始行。

整个系统正常工作并显示数据。

但是,当我单击树状视图时,整个系统的运行速度大大降低。我已经调试了。问题是在删除代码中,单击〜20 ms后删除每个QTreeWidgetItem会花费很多时间

我在Qt源中调试了它,问题似乎出在QItemSelectionModel :: setCurrentIndex

此行->发出currentChanged(d-> currentIndex,上一个);

现在,我无法进一步调试它,因为我无法确定哪个插槽连接到了该信号并放慢了速度。

我正在使用Qt 4.7.1

我在下面粘贴了整个代码:

头文件:

    #ifndef WINDOW_H
    #define WINDOW_H

    #include <QWidget>
    #include "ui_service.h"
    #include <qtimer.h>

    typedef std::pair<std::string, std::string> tSEntry;
    typedef std::vector<tSEntry> tSFifo;

    typedef struct tGUIData
    {
       std::string strCName;

       std::string strFName;
       std::string strPName;

       std::string strMName;
       std::string strMIDHex;
       std::string strMIDDec;

       std::string strTimeStamp;

       std::string strRawDataHex;
       std::string strRawDataDec;

       tSFifo oSQueue;

    } tGUIData;



    class Window : public QWidget
    {
        Q_OBJECT

       /// The Tree Widget where the data is shown
       QTreeWidget* m_pTreeHead;
       Ui::ListView* m_pForm;
       int m_nMaxItemAmount;

       QTimer m_oDataProducer;
       QTimer m_oListClearer;
    public:
        Window();
        ~Window();
    private slots:
       void ProduceData();
       void ClearTree();
       void Window::AddEntry(tGUIData& tGUIData);

    private:
       void Window::DeleteItems();

    };

    #endif

代码文件:

  #include <QtGui>

  #include "window.h"
  #include <cstdlib>
  #include <ctime>

  Window::Window()
  {
     m_pForm = new Ui::ListView();
     m_pForm->setupUi(this);

     QHeaderView* pHeader = m_pForm->treeWidget->header();
     pHeader->setResizeMode(0, QHeaderView::Interactive);
     pHeader->setResizeMode(1, QHeaderView::Interactive);
     pHeader->setResizeMode(2, QHeaderView::Interactive);
     pHeader->setResizeMode(3, QHeaderView::Interactive);
     pHeader->setResizeMode(4, QHeaderView::Stretch);

     m_pTreeHead = m_pForm->treeWidget;

     m_nMaxItemAmount = 1000;

     m_oDataProducer.start(1);
     m_oListClearer.start(10);
     connect(&m_oDataProducer, SIGNAL(timeout()), this, SLOT(ProduceData()));
     connect(&m_oListClearer, SIGNAL(timeout()), this, SLOT(ClearTree()));
  }


  std::string CreateRandomString(size_t nLength)
  {
     std::string o_str = "";
     o_str.resize(nLength);
     char cSymbols[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
     unsigned int nIndexer = 0;
     srand(static_cast<long int>(std::time(NULL)) + rand());
     while (nIndexer < nLength)
     {
        o_str[nIndexer++] = cSymbols[rand() % 62];
     }
     return o_str;
  }


  void Window::ClearTree()
  {
     DeleteItems();
  }

  void Window::ProduceData()
  {
     for (size_t i = 0; i < 500; i++)
     {

        tGUIData oGUIData;
        oGUIData.strCName = CreateRandomString(10);
        oGUIData.strFName = CreateRandomString(10);
        oGUIData.strMIDDec = CreateRandomString(10);
        oGUIData.strMIDHex = CreateRandomString(10);
        oGUIData.strMName = CreateRandomString(10);



        tSEntry oSEntry;
        oSEntry.first = CreateRandomString(10);
        oSEntry.second = CreateRandomString(10);
        oGUIData.oSQueue.push_back(oSEntry);

        AddEntry(oGUIData);
     }
  }

  Window::~Window()
  {
     m_oDataProducer.stop();
     if (m_pForm)
     {
        delete m_pForm;
        m_pForm = NULL;
     }
  }


  void Window::DeleteItems()
  {
     while (m_pTreeHead->topLevelItemCount() >= m_nMaxItemAmount)
     {
        QTreeWidgetItem* pTreeWidget = m_pTreeHead->topLevelItem(0);
        if (pTreeWidget)
        {
           delete pTreeWidget;
           pTreeWidget = NULL;
        }
     }
  }


  void Window::AddEntry(tGUIData& oGUIData)
  {
     QTreeWidgetItem* pMessageItem = new QTreeWidgetItem(m_pTreeHead);

     pMessageItem->setText(0, oGUIData.strCName.c_str());
     pMessageItem->setText(1, oGUIData.strMName.c_str());
     pMessageItem->setText(2, oGUIData.strTimeStamp.c_str());
     pMessageItem->setText(3, oGUIData.strRawDataDec.c_str());
     pMessageItem->setText(4, oGUIData.strPName.c_str());

     while (!oGUIData.oSQueue.empty())
     {
        QTreeWidgetItem* pSubItem = new QTreeWidgetItem(pMessageItem);
        tSEntry& sEntry = oGUIData.oSQueue.front();
        pSubItem->setText(1, sEntry.first.c_str());
        pSubItem->setText(2, sEntry.second.c_str());

        oGUIData.oSQueue.erase(oGUIData.oSQueue.begin());
     }

  }

1 个答案:

答案 0 :(得分:0)

删除项目时,禁用更新,如下所示:

QTreeWidget tw;
tw.setUpdatesEnabled(false);

要删除所有项目,请使用(如果项目不包含指针):

tw.clear()

您不需要在QTreeWidget析构函数之前删除项目。