GTKMM buttons in a grid - c++

just learned the concept of GTKMM and found out that I enjoyed this, compared to just C++, which is pretty boring just programming in the terminal. As I decided to create a code that will make a 8 x 8 buttons of table, I encountered a problem, which is how to set every button to my desired position, because the code I created just makes 64 buttons horizontally. Can anyone help me here? Thanks
examplewindow.h (The code the class attributes and methods)
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
private:
// Signal handlers:
void on_button_numbered(const Glib::ustring& data);
// Child widgets:
Gtk::Grid m_grid;
Gtk::Button button[8][8];
};
#endif /* GTKMM_EXAMPLEWINDOW_H */
examplewindow.cc (The code for modifying the attributes and methods)
#include <iostream>
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
{
set_title("Mr. Sandman");
set_border_width(12);
int i, j;
add(m_grid);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
m_grid.add(button[i][j]);
}
}
for (int i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
button[i][j].signal_clicked().connect(
sigc::bind<Glib::ustring>( sigc::mem_fun(*this,
&ExampleWindow::on_button_numbered), "button 1") );
}
}
m_grid.show_all();
}
ExampleWindow::~ExampleWindow()
{
}
void
ExampleWindow::on_button_numbered(const Glib::ustring& data)
{
std::cout << data << " was pressed" << std::endl;
}
main.cc (The main function)
#include "examplewindow.h"
#include <gtkmm/application.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
ExampleWindow window;
// Shows the window and returns when it is closed.
return app->run(window);
}

instead of using m_Grid.add() try using attach() or attach_next_to()
so you can either give row/column position in your loop or attach button 9 below button 1 and button 2 to the right of button 1
https://developer.gnome.org/gtkmm/unstable/classGtk_1_1Grid.html#a9c425e95660daff60a77fc0cafc18115

Related

How can I declare a SFML window to all my class functions?

This might be a noob question, but I'm trying to make a simple player move in a 2D-grid in SFML. I'm creating a while loop for rendering what is happening, and I can get it to work, but I want to use classes for the grid and players etc. The problem is that when I create a window called 'window', I don't know how to implement classes since these don't know what the 'window' is. I hope I have described my problem sufficiently, and I would like any respons on how to make this work or if my method of doing it is already bad and should be changed for another method. Here is a snippet of my code for the class and the undeclared window error.
class myEvents {
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;
//Function to create a grid with RectangleShapes
void grid() {
for (int i = 0; i < tileCount; i++)
{
for (int j = 0; j < tileCount; j++)
{
sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile); //Problem occurs here, 'window' is not declared, it is in the next function
//window.draw(tile); must execute in the loop to render a full grid
}
}
}
//Includes while loop for rendering and events. Could be written without class, but I'd still like a class for the grid and later on a player.
//So I need the window to work with my classes.
void loop() {
sf::RenderWindow window(sf::VideoMode(width, height), "Game"); //'window' declared here. Can I move this statement
//somewhere so that my funcions know where it comes from?
while (window.isOpen) {
sf::Event event;
while (window.pollEvent(e))
{
//to be further developed
}
}
}
};
Try making the window a class member:
class myEvents {
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;
sf::RenderWindow window{sf::VideoMode(width, height), "Game"};
void grid() {
for (int i = 0; i < tileCount; i++)
{
for (int j = 0; j < tileCount; j++)
{
sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile);
}
}
}
void loop() {
while (window.isOpen) {
sf::Event event;
while (window.pollEvent(e))
{
//to be further developed
}
}
}
};
Alternatively, pass the window around:
class myEvents {
public:
//Variables
int tSize = 40;
int tileCount = 20;
int width = tileCount * tSize;
int height = tileCount * tSize;
void grid(sf::RenderWindow& window) {
for (int i = 0; i < tileCount; i++)
{
for (int j = 0; j < tileCount; j++)
{
sf::RectangleShape tile(sf::Vector2f(40, 40));
tile.setFillColor(sf::Color::Magenta);
tile.setPosition(i*tSize, j*tSize);
window.draw(tile);
}
}
}
void loop() {
sf::RenderWindow window{sf::VideoMode(width, height), "Game"};
while (window.isOpen) {
sf::Event event;
while (window.pollEvent(e))
{
//to be further developed
// call grid(window)
}
}
}
};
Well an easy thing for the window you can do is to create a class and
add the window in it, then include the .h where the class is as you've set the sf::window in the public section. Now I will show you how you can use it
we've got an .h file named window.h (you can name it whatever is comfortable for you)
#include <SFML/Grpahics.hpp>
class window
{
public:
sf::RenderWindow window;
}
and you have the main.cpp(or whatever file you want)
#include "window.h"
window w;
int main()
{
w.window.create(sf::VideoMode(800,600),"Title");
}
the int main() can be whatever function you want it just has to be
called as soon as possible so the window will show.
I hope this helps you with your problem =)
[EDID]:
As the "sf::RenderWindow window" is public every file that has the header with the public "sf::RenderWindow window" can use the window as it is not private. I think you know that but in any case to add it.

QGraphicsView doesn't always update

Hi I've got a GridLayout which has 64 GraphicsViews on it (I know it's alot but it's the only way i could think of doing this at this point in time).
Now i'm currently just drawing a random line on each of these graphics views on a timer tick. This works but only for the 8 of the Graphics,
Create Graphics Views
void Simulation::createGraphicsViews(){
for(int i = 0; i < 64; i++){
for(int j = 0; j < 8; j++){
graphicsScene[i] = new QGraphicsScene();
graphicsView[i] = new QGraphicsView(graphicsScene[i]);
simui->gridLayout->addWidget(graphicsView[i], i/8, j);
}
}
}
Random Line in each graphics view
for(int x = 0; x < 64; x++){
x1 = qrand()%(50+1) - 1;
y1 = qrand()%(50+1)-1;
x2 = qrand()%(50+1)-1;
y2 = qrand()%(50+1)-1;
graphicsScene[x]->addLine(x1,y1,x2,y2);
qDebug() << "adding line to" << x << "at" << x1 <<","<<y1<<","<<x2<<","<<y2;
}
show updated graphics view
for(int x = 0; x < 64; x++){
graphicsView[x]->show();
qDebug()<<"showing" << x;
}
I've looked through it for the last 2 hours trying multiple approaches none of which have fixed this problem, I'm assuming it's probably something stupid but I just can't figure it out
Any help is greatly appreciated
Thank you
Also if i try to update any of the Graphics Views other than the ones which work they still don't update.
https://gist.github.com/gazza126/f43d5b0377649782a35d -- Full Code (that does anything)
The below works. Make sure that you enable C++11 in your .pro file: add CONFIG += c++11 to the project file.
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <QGridLayout>
#include <QTime>
#include <QTimer>
#include <array>
class View : public QGraphicsView
{
public:
View(QWidget *parent = 0) : QGraphicsView(parent) {
setRenderHint(QPainter::Antialiasing);
}
void resizeEvent(QResizeEvent *) {
fitInView(-1, -1, 2, 2, Qt::KeepAspectRatio);
}
};
template <typename Container>
void updateScenes(Container & views)
{
auto angle = 360.0/1000.0 * (QTime::currentTime().msecsSinceStartOfDay() % 1000);
for (auto & view : views) {
auto scene = view.scene();
scene->clear();
auto * line = scene->addLine(-1, 0, 1, 0, QPen(Qt::darkBlue, 0.1));
line->setRotation(angle);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene s;
QTimer timer;
QWidget window;
QGridLayout layout(&window);
std::array<View, 64> views;
int i = 0;
for (auto & view : views) {
view.setScene(new QGraphicsScene(&view));
layout.addWidget(&view, i/8, i%8);
++ i;
}
QObject::connect(&timer, &QTimer::timeout, [&views]{ updateScenes(views); });
timer.start(50);
window.show();
return a.exec();
}

QT QStandardItemModel segfaulting

I'm pretty new to QT, and I'm trying to take a list from a text file and output it into QT with nice formatting.
I managed to get the list printed on the window, but it has to be able to be sorted.
I have the radio buttons set up right now so that one of them displays the list and the other clears the list.
The problem is that when I switch from the list to the cleared list back to the list the program segfaults and I don't understand why.
The files are here.
winelist.cpp
#include "winelist.h"
#include "ui_winelist.h"
#include <QFile>
#include <QString>
#include <QStandardItemModel>
wineList::wineList(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::wineList)
{
ui->setupUi(this);
ui->ratingButton->setChecked(true);
fillList();
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Wine Name")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Vintage")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Rating")));
model->setHorizontalHeaderItem(3, new QStandardItem(QString("Price")));
ui->listOutput->setModel(model);
}
wineList::~wineList()
{
delete ui;
}
void wineList::on_sortButton_clicked()
{
if( ui->ratingButton->isChecked())
{
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
model->setItem(i,1,wList[i].vintage);
model->setItem(i,2,wList[i].rating);
model->setItem(i,3,wList[i].price);
}
}
else
{
for(int i = 0; i < 100; i++) {
for(int j = 0; j < 4; j++) {
model->setItem(i, j, new QStandardItem(QString("")));
}
}
}
ui->listOutput->resizeColumnsToContents();
ui->listOutput->resizeRowsToContents();
}
void wineList::fillList()
{
Wine wine;
QString line;
QStringList lineElements;
QFile wineText(":/winelist.txt");
if (wineText.open(QIODevice::ReadOnly))
{
while ((line = line.fromUtf8(wineText.readLine())) != "")
{
lineElements = line.split(";");
lineElements[0].replace("\t", "");
lineElements[1].replace("\t", "");
wine.wineName = new QStandardItem(QString(lineElements.at(0)));
wine.vintage = new QStandardItem(QString(lineElements.at(1)));
wine.rating = new QStandardItem(QString::number(lineElements.at(2).toInt()));
wine.price = new QStandardItem(QString::number(lineElements.at(3).toInt()));
wList.append(wine);
}
}
wineText.close();
}
winelist.h
#ifndef WINELIST_H
#define WINELIST_H
#include <QMainWindow>
#include <QStandardItem>
#include <QStandardItemModel>
namespace Ui {
class wineList;
}
struct Wine {
QStandardItem* wineName;
QStandardItem* vintage;
QStandardItem* rating;
QStandardItem* price;
};
class wineList : public QMainWindow
{
Q_OBJECT
public:
explicit wineList(QWidget *parent = 0);
~wineList();
private slots:
void on_sortButton_clicked();
private:
Ui::wineList *ui;
QVarLengthArray<Wine> wList;
QStandardItemModel *model = new QStandardItemModel(100, 4, this);
void fillList();
void printList(QStandardItemModel *model);
};
#endif // WINELIST_H
main.cpp
#include "winelist.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
wineList w;
w.show();
return a.exec();
}
Clicking sort the first time
Switching Radio Button and clicking sort again
Switching Radio Button back and clicking sort again
Any Help is appreciated, I am completely lost here.
In the on_sortButton_clicked you're trying to read data from a list, but not doing any range checks. Instead, you've hardcoded 100 there.
You should rewrite this:
for (int i = 0; i < 100; i++) {
model->setItem(i,0,wList[i].wineName);
to this:
for (int i = 0; i < wList.size(); i++) {
model->setItem(i,0,wList[i].wineName);
--upd---
When you initially populate your model, it takes ownership over items from wList. When you replace model items with empty ones, it deletes initial items from wList. After this your wList is no move valid, because it contains Wine structs with dangling pointers. That's why when you try to populate your model second time, it crashes.

My array of sprites is not getting drawn on my renderwindow

So I am trying to make a chessgame, and of course I am starting out with just making the board. For this I am creating a two-dimensional array like 8x8. Each tile has their own object sprite called tileSprite and I am trying to define them as 1/8th of the width and height of the screen, which has a fixed size of 1024x768. The textures that are loaded in are just 1 pixel black or white. The program gives no definitive error, but the drawn tiles aren't getting displayed onto my window, and all I see is a red screen, which I get from clearing the screen (following the standard clear/draw/display cycle of SFML).
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
#include <iostream>
#include <vector>
#include <array>
bool playing = true;
class Textures {
public:
static void loadTextures();
sf::Texture static blackTile;
sf::Texture static whiteTile;
};
/*---Class--Definitions----*/
sf::Texture Textures::blackTile;
sf::Texture Textures::whiteTile;
/*-------------------------*/
void Textures::loadTextures() {
if (blackTile.loadFromFile("Images/blackTile.PNG")) {
return;
}
else if (whiteTile.loadFromFile("Images/whiteTile.PNG")) {
return;
}
}
class ChessBoard_Tiles {
public:
static void _initialize();
static void draw(sf::RenderWindow& window);
enum TileStatus { EMPTY, OCCUPIED, HIGHLIGHTED };
struct TileSlot {
sf::Sprite tileSprite;
TileStatus tileStatus;
};
private:
typedef std::array< std::array<TileSlot*, 8>, 8 > TileType;
static TileType tileBoard;
};
/*-Class Definition that has to be called-*/
ChessBoard_Tiles::TileType ChessBoard_Tiles::tileBoard = ChessBoard_Tiles::TileType();
void ChessBoard_Tiles::_initialize() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
tileBoard[i][j] = new TileSlot();
if ((i + j) % 2 == 0) {
tileBoard[i][j]->tileSprite.setTexture(Textures::whiteTile);
}
else {
tileBoard[i][j]->tileSprite.setTexture(Textures::blackTile);
}
tileBoard[i][j]->tileSprite.setPosition(128 * j, 96 * i);
tileBoard[i][j]->tileSprite.setScale(128, 96);
tileBoard[i][j]->tileStatus = EMPTY;
}
}
}
void ChessBoard_Tiles::draw(sf::RenderWindow& window) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
window.draw(tileBoard[i][j]->tileSprite);
}
}
}
int main(int argc, char* argv[]) {
sf::RenderWindow renderWindow;
renderWindow.create(sf::VideoMode(1024, 768), "Chess");
Textures::loadTextures();
ChessBoard_Tiles::_initialize();
while (playing) {
sf::Event currentEvent;
renderWindow.pollEvent(currentEvent);
if (currentEvent.type == sf::Event::Closed) {
renderWindow.close();
playing = false;
}
if (currentEvent.type == sf::Event::KeyPressed) {
if (currentEvent.key.code == sf::Keyboard::Escape) {
renderWindow.close();
playing = false;
}
}
renderWindow.clear(sf::Color::Red);
std::cout << "Clear\n";
ChessBoard_Tiles::draw(renderWindow);
std::cout << "Draw\n";
renderWindow.display();
std::cout << "Display\n";
}
return 0;
}
If anyone knows what is wrong then that would be a big help for me.. Otherwise I'd probably have to entirely remake this program, which I think should work so far, but I haven't the slightest idea why it doesn't. Big thanks in advance =)
EDIT: Read a little about how the sprites seem to disappear if the textures are running out of scope.. If this is the case, could that just be pointed out and then I can work on that, because I have tried so many different things by now that I feel like I'm just going in circles?
The fault was from the loadTextures() function, where I had forgotten to include the file extension of the file to be loaded, and I screwed the logic about the loadFromFile() function up so the program only continued when it couldn't load the file..

Benchmarking QTableWidget for multi threaded input, strange result?

In my program I am reading a lot of data from a text file & putting it to a QTableWidget.
Initially it was all done in the GUI thread & then I decided to multi thread it to have better performance. But on the contrary the performance was significantly slow i.e. 8x slower!
So I decided to benchmark it. Here are my files:
main.cpp
#include <QtGui/QApplication>
#include <QMutex>
#include <QDir>
#include <string>
#include <QDebug>
#include "mainwindow.h"
QFile *logFile = NULL;
QTextStream *logStream = NULL;
QMutex *mutex = NULL;
bool *debugMode = NULL;
void myMessageOutput(QtMsgType type, const char *msg)
{
if(((logFile != NULL) && (debugMode != NULL)))
{
mutex->lock();
switch (type)
{
case QtDebugMsg:
if(!*debugMode)
{
mutex->unlock();
return;
}
*logStream << msg;
logStream->flush();
break;
case QtWarningMsg:
*logStream << "\n*** Warning ***\n";
*logStream << msg;
*logStream << "\n*** Warning Complete ***\n";
logStream->flush();
break;
case QtCriticalMsg:
*logStream << "\n*** Critical ***\n";
*logStream << msg;
*logStream << "\n*** Critical Complete ***\n";
logStream->flush();
break;
case QtFatalMsg:
*logStream << "\n*** Fatal ***\n";
*logStream << msg;
*logStream << "\n*** Fatal Complete ***\n";
logStream->flush();
abort();
}
mutex->unlock();
}
}
void CreateLogFile()
{
QString path = "C:\\Users\\abcd\\Documents\\QT\\benchmark\\output.log";
QFile *file = new QFile(path);
if(file->exists())
file->remove();
if(!file->open(QFile::WriteOnly | QFile::Text))
{
qFatal("Could not create log file.");
}
logStream = new QTextStream(file);
logStream->setRealNumberNotation(QTextStream::FixedNotation);
logStream->setRealNumberPrecision(16);
logFile = file;
}
int main(int argc, char *argv[])
{
mutex = new QMutex();
qInstallMsgHandler(myMessageOutput);
debugMode = new bool;
CreateLogFile();
*debugMode = true;
QApplication a(argc, argv);
MainWindow w;
w.show();
w.bench2();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread>
#include "multi_thread.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void bench2();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QList>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::bench2()
{
QTableWidget *table = new QTableWidget();
table->setRowCount(1000);
table->setColumnCount(1000);
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j < 1000; j++)
table->setItem(i, j, new QTableWidgetItem());
}
Multi_Thread **multis = new Multi_Thread *[4];
QThread **thrs = new QThread *[4];
int from;
int to = -1;
QTime time;
time.start();
for(int i = 0; i < 4; i++)
{
from = to + 1;
to = from + 250;
if(i == 3)
to = 999;
multis[i] = new Multi_Thread();
multis[i]->setTable(table, from, to);
thrs[i] = new QThread();
connect(thrs[i], SIGNAL(started()), multis[i], SLOT(bench2_1()));
multis[i]->moveToThread(thrs[i]);
}
for(int i = 0; i < 4; i++)
{
if(!i)
time.start();
thrs[i]->start();
}
for(int i = 0; i < 4; i++)
{
thrs[i]->wait();
}
qDebug() << "\nbench2 1 " << time.elapsed();
for(int i = 0; i < 4; i++)
{
delete multis[i];
delete thrs[i];
}
delete[] multis;
delete[] thrs;
table->clear();
table->setRowCount(1000);
table->setColumnCount(1000);
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j < 1000; j++)
table->setItem(i, j, new QTableWidgetItem());
}
time.start();
for(int i = 0; i < 1000; i++)
{
for(int j = 0; j < 1000; j++)
table->item(i, j)->setText("0");
}
qDebug() << "\nbench2 2 " << time.elapsed();
table->clear();
table->setRowCount(1000);
table->setColumnCount(1000);
QTableWidgetItem ***items = new QTableWidgetItem **[1000];
for(int i = 0; i < 1000; i++)
{
items[i] = new QTableWidgetItem *[1000];
for(int j = 0; j < 1000; j++)
{
QTableWidgetItem *item = new QTableWidgetItem();
table->setItem(i, j, item);
items[i][j] = item;
}
}
multis = new Multi_Thread *[4];
thrs = new QThread *[4];
to = -1;
for(int i = 0; i < 4; i++)
{
from = to + 1;
to = from + 250;
if(i == 3)
to = 999;
multis[i] = new Multi_Thread();
multis[i]->setItems(items, from, to);
thrs[i] = new QThread();
connect(thrs[i], SIGNAL(started()), multis[i], SLOT(bench2_2()));
multis[i]->moveToThread(thrs[i]);
}
table->blockSignals(true);
table->setUpdatesEnabled(false);
table->setWordWrap(false);
for(int i = 0; i < 4; i++)
{
if(!i)
time.start();
thrs[i]->start();
}
for(int i = 0; i < 4; i++)
{
thrs[i]->wait();
}
qDebug() << "\nbench2 3 " << time.elapsed();
table->blockSignals(false);
table->setUpdatesEnabled(true);
table->setWordWrap(true);
for(int i = 0; i < 4; i++)
{
delete multis[i];
delete thrs[i];
}
delete[] multis;
delete[] thrs;
table->clear();
for(int i = 0; i < 1000; i++)
{
delete[] items[i];
}
delete[] items;
}
multi_thread.h
#ifndef MULTI_THREAD_H
#define MULTI_THREAD_H
#include <QObject>
#include <QThread>
#include <QTableWidget>
class Multi_Thread : public QObject
{
Q_OBJECT
public:
explicit Multi_Thread();
void setTable(QTableWidget *tab, int f, int t);
void setItems(QTableWidgetItem ***i, int f, int t);
private:
QTableWidget *table;
QTableWidgetItem ***items;
int from;
int to;
signals:
public slots:
void bench2_1();
void bench2_2();
};
#endif // MULTI_THREAD_H
multi_thread.cpp
#include "multi_thread.h"
Multi_Thread::Multi_Thread() : QObject()
{
}
void Multi_Thread::setTable(QTableWidget *tab, int f, int t)
{
table = tab;
from = f;
to = t;
}
void Multi_Thread::setItems(QTableWidgetItem ***i, int f, int t)
{
items = i;
from = f;
to = t;
}
void Multi_Thread::bench2_1()
{
for(int i = from; i <= to; i++)
{
for(int j = 0; j < 1000; j++)
{
table->item(i, j)->setText("0");
}
}
QThread::currentThread()->exit(0);
}
void Multi_Thread::bench2_2()
{
for(int i = from; i <= to; i++)
{
for(int j = 0; j < 1000; j++)
{
items[i][j]->setText("0");
}
}
QThread::currentThread()->exit(0);
}
output.log
bench2 1 7654
bench2 2 1160
bench2 3 8021
What is strange is that I was expecting "bench2 3" to be faster than "bench2 1".
PS: My laptop hardware can needs 4 threads to reach 100% usage. Please edit it as per your hardware requirements. Can be known from Environment variables.
Calling table->item(...)->setText() from any thread other than the GUI thread is undefined behavior. Don't do it. It's nice that you're using a QObject in a QThread to do the job, but you must not directly call methods on objects that live in other threads.
The internal model will call the view's dataChanged() slot for each of your updates. This is the likely source of the slow-down, apart from the memory allocations for each item.
The typical way of preparing a data model in a separate thread would be to:
Instantiate the model in a separate thread. The model must not yet be connected to any views at this point. Initialize the model with data. It can be a QStandardItemModel or a custom model. Custom models are not that hard, and can often be much more efficient if you don't need to perform a memory allocation per each item.
Connect the model to the view (QTableView, not QTableWidget). You can move it to the gui thread for convenience, although you don't have to.
Only access the model from the thread it lives in, unless you use queued method invocations or queued signal-slot connections. The view uses the latter and thus can live in a thread separate from the model.
A QTableWidget bundles an internal QStandardItemModel with a QTableView. If you want to deal with the model separately, you simply use the the model class in isolation from the view class.
This answer has a different strategy for getting large amounts of data into a QTableView very efficiently (time and memory). I also cached a parsed row of data for successive requests to the same row.