如何摆脱listview和listWidgets闪烁?

时间:2013-12-23 11:58:46

标签: c++ listview qt5 flicker qlistwidget

我正在尝试以<1毫秒的间隔获取进程列表,然后在listwidgetlistview中显示最新列表。
listWidget中发生了很多闪烁! 我试过listview它更糟糕!
这是我的示例代码:

#include "frmprocess.h"
#include "ui_frmprocess.h"
#include "qprocess.h"
#include <windows.h>
#include <tlhelp32.h>
#include "qstring.h"
#include "qtimer.h"
#include "qlist.h"
#include "qmessagebox.h"

frmProcess::frmProcess(QWidget *parent) :  QMainWindow(parent),  ui(new Ui::frmProcess)
{
    ui->setupUi(this);

    //setting timer
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(OnTimerTick()));

    model = new QStringListModel(this);

}

frmProcess::~frmProcess()
{
    delete ui;
}
//in each timer tick event, we get the latest list of running processes
void frmProcess::OnTimerTick()
{
    LatestProcessList.clear();
    LatestProcessList.append(GetAllRunningProcesses());

    if(ui->checkBox->isChecked())
    {
        //getting new processes which are not in the white-list
        GetDifference();
    }
   ui->listWidget->addItems(GetAllRunningProcesses());

    //Filling model! in order to avoid flickering!-- this doesnt help!
     model->setStringList(LatestProcessList);
     ui->listView->setModel(model);

}

//This is where we create a white-list out of our latest running process list
void frmProcess::on_pushButton_clicked()
{
   // GetAllRunningProcesses();
    WhiteList.clear();
    ui->listWidget_2->clear();

    WhiteList.append(GetAllRunningProcesses());
    ui->listWidget_2->addItems(WhiteList);
}
//This method gets the list of all running processes
QList<QString> frmProcess::GetAllRunningProcesses()
{
    HANDLE hSysSnapshot = NULL;
    PROCESSENTRY32 proc;

    QList<QString> list;
    proc.dwSize = sizeof(proc);
    hSysSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
    Process32First(hSysSnapshot,&proc);
    proc.dwSize = sizeof(proc);

    ui->listWidget->clear();
    list.clear();

    do
    {
        list.append(QString::fromWCharArray(proc.szExeFile));
    } while(Process32Next(hSysSnapshot,&proc));

    CloseHandle( hSysSnapshot );
    return list;
}

void frmProcess::GetDifference()
{
   QSet<QString> intersection = LatestProcessList.toSet().subtract(WhiteList.toSet());
   ui->listWidget_3->clear();
   ui->listWidget_3->addItems(intersection.toList());
}

void frmProcess::on_btnStartTimer_clicked()
{
    if(ui->btnStartTimer->text()=="Start")
    {
        timer->start(1);
        ui->btnStartTimer->setText("Stop");
    }
    else
    {
        timer->stop();
        ui->btnStartTimer->setText("Start");
    }
}

0 个答案:

没有答案