qt内存问题QTable小部件

时间:2017-08-22 07:35:18

标签: qt qt-creator qtablewidget qtablewidgetitem

我已经安装了Qt Creator并创建了一个项目,它将在QTableWidget中显示大量数据。我的内存使用有一些问题。我的项目在显示表数据时使用~2 GB的RAM。有近140万行,每行有8列。

如何解决这个问题?我用于创建表的过程是否错误?

我的代码:

main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    t1 = new QTableWidgetItem();
    t2 = new QTableWidgetItem();
    t3 = new QTableWidgetItem();
    t4 = new QTableWidgetItem();
    t5 = new QTableWidgetItem();
    t6 = new QTableWidgetItem();
    t7 = new QTableWidgetItem();
    t8 = new QTableWidgetItem();
    qDebug()<<sizeof(t1);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    delete t1;
    delete t2;
    delete t3;
    delete t4;
    delete t5;
    delete t6;
    delete t7;
    delete t8;
    unsigned int index =0;
    ui->tableWidget->setRowCount(1426500);
    while (index < 1426500)
    {
        t1 = new QTableWidgetItem();
        t1->setText(QString::number(index));
        ui->tableWidget->setItem(index,0,t1);
        ui->tableWidget->item(index,0)->setBackgroundColor(QColor(180,150,220));

        t2 = new QTableWidgetItem();
        t2->setText(QString::number(index));
        ui->tableWidget->setItem(index,1,t2);
        ui->tableWidget->item(index,1)->setBackgroundColor(QColor(180,150,220));

        t3 = new QTableWidgetItem();
        t3->setText(QString::number(index));
        ui->tableWidget->setItem(index,2,t3);
        ui->tableWidget->item(index,2)->setBackgroundColor(QColor(180,150,220));

        t4 = new QTableWidgetItem();
        t4->setText(QString::number(index));
        ui->tableWidget->setItem(index,3,t4);
        ui->tableWidget->item(index,3)->setBackgroundColor(QColor(180,150,220));

        t5 = new QTableWidgetItem();
        t5->setText(QString::number(index));
        ui->tableWidget->setItem(index,4,t5);
        ui->tableWidget->item(index,4)->setBackgroundColor(QColor(180,150,220));

        t6 = new QTableWidgetItem();
        t6->setText(QString::number(index));
        ui->tableWidget->setItem(index,5,t6);
        ui->tableWidget->item(index,5)->setBackgroundColor(QColor(180,150,220));

        t7 = new QTableWidgetItem();
        t7->setText(QString::number(index));
        ui->tableWidget->setItem(index,6,t7);
        ui->tableWidget->item(index,6)->setBackgroundColor(QColor(180,150,220));

        t8 = new QTableWidgetItem();
        t8->setText(QString::number(index));
        ui->tableWidget->setItem(index,7,t8);
        ui->tableWidget->item(index,7)->setBackgroundColor(QColor(180,150,220));
        index++;

    }
}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTableWidgetItem>
#include <QDebug>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QTableWidgetItem *t1;
    QTableWidgetItem *t2;
    QTableWidgetItem *t3;
    QTableWidgetItem *t4;
    QTableWidgetItem *t5;
    QTableWidgetItem *t6;
    QTableWidgetItem *t7;
    QTableWidgetItem *t8;

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1122</width>
    <height>572</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>0</y>
      <width>99</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>PushButton</string>
    </property>
   </widget>
   <widget class="QTableWidget" name="tableWidget">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>50</y>
      <width>1031</width>
      <height>441</height>
     </rect>
    </property>
    <property name="columnCount">
     <number>8</number>
    </property>
    <attribute name="horizontalHeaderVisible">
     <bool>false</bool>
    </attribute>
    <column/>
    <column/>
    <column/>
    <column/>
    <column/>
    <column/>
    <column/>
    <column/>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1122</width>
     <height>25</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

QTableTest.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2017-08-12T12:22:34
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QTableTest
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

0 个答案:

没有答案