想要在QThread中更新QtableWidget

Want to update a QtableWidget within a QThread

本文关键字:更新 QtableWidget QThread      更新时间:2023-10-16

我正在启动一个名为Nice System Monitor的项目,旨在监控Linux上的进程,我正在将C++和Qt与QtCreator一起使用。

我已经开始制作一个QThread,其中调用了一个函数来重复填充QTableWidget,但即使在再次填充之前删除了每一行,表也不会正确更新。

我对Qt很陌生,并从互联网上的不同来源中获得灵感。

这是QThread的代码:

#include <unistd.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
#include <stdio.h>
#include <cctype>
#include <stdlib.h>
#include <vector>
#include <sstream>
#include "renderprocesstablethread.h"
#include <proc/readproc.h>
#include <proc/procps.h>
#include "mainwindow.h"
using namespace std;

RenderProcessTableThread::RenderProcessTableThread(QObject *parent)
    : QThread(parent)
{
    restart = false;
    abort = false;
}
RenderProcessTableThread::~RenderProcessTableThread()
{
    mutex.lock();
    abort = true;
    condition.wakeOne();
    mutex.unlock();
    wait();
}
bool RenderProcessTableThread::isNum(char *s) {
    int i = 0,  flag;
    while(s[i]){
            //if there is a letter in a string then string is not a number
        if(isalpha(s[i]) || s[i] == '.'){
            flag = 0;
            break;
        }
        else flag = 1;
        i++;
        }
    if (flag == 1) return true;
    else return false;
}
string RenderProcessTableThread::convertDouble(double value) {
  std::ostringstream o;
  if (!(o << value))
    return "";
  return o.str();
}
string RenderProcessTableThread::convertInt(int value) {
  std::ostringstream o;
  if (!(o << value))
    return "";
  return o.str();
}

void RenderProcessTableThread::run()
{
    forever {
        mutex.lock();
        mutex.unlock();
        fillProcessTable();
        sleep(1000);
        //cout << "ça marche" << endl;
    }
    mutex.lock();
    if (!restart)
        condition.wait(&mutex);
    restart = false;
    mutex.unlock();
}

void RenderProcessTableThread::setLocalMainWindow(MainWindow& w)
{
    localMainWindow = &w;
    ui_tableWidgetProcessus = localMainWindow->findChild<QTableWidget*>("tableWidgetProcessus");
    ui_tableWidgetProcessus->setColumnCount(11);
    ui_tableWidgetProcessus->setColumnWidth(10,508);
    QFont fnt;
    fnt.setPointSize(10);
    fnt.setFamily("Arial");
    ui_tableWidgetProcessus->setFont(fnt);
    QStringList labels;
    labels << "user" << "pid" << "cpu" << "nice" << "vsz" << "rss" << "tty" << "stat" << "start" << "time" << "cmd";
    ui_tableWidgetProcessus->setHorizontalHeaderLabels(labels);
}

void RenderProcessTableThread::fillProcessTable() {
    QMutexLocker locker(&mutex);
    if (!isRunning()) {
        start(LowPriority);
    } else {
        restart = true;
        condition.wakeOne();
    }
    PROCTAB* proc = openproc(PROC_FILLUSR | PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS | PROC_FILLARG);
    proc_t proc_info;
    memset(&proc_info, 0, sizeof(proc_info));
    int totalRow = ui_tableWidgetProcessus->rowCount();
    for ( int i = 0; i < totalRow ; ++i )
    {
           ui_tableWidgetProcessus->removeRow(i);
    }
    int i = 0;
    while (readproc(proc, &proc_info) != NULL) {
      cout << proc_info.fuser << proc_info.tid << proc_info.cmd << proc_info.resident << proc_info.utime << proc_info.stime << endl;
      ui_tableWidgetProcessus->setRowCount(i+1);
      ui_tableWidgetProcessus->setItem(i,0,new QTableWidgetItem(QString(proc_info.fuser),0));
      ui_tableWidgetProcessus->setItem(i,1,new QTableWidgetItem(QString((char*)convertInt(proc_info.tid).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,2,new QTableWidgetItem(QString((char*)convertInt(proc_info.pcpu).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,3,new QTableWidgetItem(QString((char*)convertInt(proc_info.nice).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,4,new QTableWidgetItem(QString((char*)convertInt(proc_info.vm_size).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,5,new QTableWidgetItem(QString((char*)convertInt(proc_info.rss).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,6,new QTableWidgetItem(QString((char*)convertInt(proc_info.tty).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,7,new QTableWidgetItem(QString(proc_info.state),0));
      ui_tableWidgetProcessus->setItem(i,8,new QTableWidgetItem(QString((char*)convertInt(proc_info.start_time).c_str()),0));
      ui_tableWidgetProcessus->setItem(i,9,new QTableWidgetItem(QString((char*)convertInt(proc_info.stime).c_str()),0));
      //cout << "proc_info.tid : " << proc_info.tid << endl;
      //cout << "proc_info.cmdline : " << proc_info.cmdline << endl;
      string text;
      if (proc_info.cmdline != 0) {
        vector<string> v(proc_info.cmdline, proc_info.cmdline + sizeof(proc_info.cmdline) / sizeof(string));
        text = v[0];
      }
      else {
        vector<string> v;
        v.push_back(proc_info.cmd);
        text = v[0];
      }
      //string text = char_to_string(proc_info.cmdline);
      ui_tableWidgetProcessus->setItem(i,10,new QTableWidgetItem(QString((char*)text.c_str()),0));
      i++;
    }
    closeproc(proc);
}

他们有更好的方法吗?

感谢

Patrick

这看起来像是Qt的Signal和Slots。在您的情况下,线程会发出信号,并且会调用窗口中的一个插槽。

因此,在RenderProcessTableThread.h中定义一个信号

signals:
void newValues(const QString &data);

在你的主窗口.h

public slots:
void showNewValues(const QString &data);

将数据添加到此插槽中的表中。然后你必须连接它们(例如,在创建线程后,在主窗口的构造函数中)

connect(yourThread, SIGNAL(newValues(QString)), this, SLOT(showNewValues(QString)));

每当你想显示新数据时,发出信号(例如fillProcessTable()函数中的某个地方):

emit newValues(yourValues);

Qt为您完成线程之间的连接。