Sorry, I'm stuck again. This time I can't get the QTableWidget to repaint.
The program below demonstrates the trouble I've been having. Originally I had it so that whenever the button is clicked, a new image will get added to my tableWidget. In order to get the image to display each time, I had to call tableWidget->update() and tableWidget->setFocus().
However, if I add images to my tableWidget from a timer (which it's doing now), any combination of update(), repaint(), and setFocus() has no effect. It only repaints when I scroll the table or click on a cell. Any help would be much appreciated. Thanks!
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
public:
void pushImage
(const QImage &image
) {
imageVector.push_back(image);
}
if (index.column() < imageVector.size()) {
painter->drawImage(option.rect.x(),option.rect.y(),imageVector.at(index.column()));
}
}
private:
};
{
Q_OBJECT
public:
~MainWindow();
private slots:
void add_image();
private:
Delegate *imageDelegate;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
class Delegate : public QItemDelegate {
public:
Delegate(QObject *parent=0) : QItemDelegate(parent){ }
void pushImage (const QImage &image)
{
imageVector.push_back(image);
}
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QItemDelegate::paint(painter, option, index);
if (index.column() < imageVector.size()) {
painter->drawImage(option.rect.x(),option.rect.y(),imageVector.at(index.column()));
}
}
private:
QVector <QImage> imageVector;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void add_image();
private:
QTableWidget *tableWidget;
QPushButton *button;
Delegate *imageDelegate;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp (need to replace /path/to/image with a valid path)
#include "mainwindow.h"
#include <QtGui>
MainWindow
::MainWindow(QWidget *parent
){
setCentralWidget(widget);
tableWidget->setColumnCount(7);
tableWidget->setRowCount(1);
imageDelegate = new Delegate(tableWidget);
tableWidget->setItemDelegate(imageDelegate);
for (int i = 0; i < tableWidget->columnCount(); i++) {
tableWidget->setColumnWidth(i,400);
}
tableWidget->setRowHeight(0,400);
layout->addWidget(tableWidget);
layout->addWidget(button);
widget->setLayout(layout);
setMinimumSize(160,160);
resize(1000, 800);
QObject::connect(button,
SIGNAL(clicked
()),
this,
SLOT(add_image
()));
connect(timer, SIGNAL(timeout()), this, SLOT(add_image()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
}
void MainWindow::add_image()
{
QImage newImage
("/path/to/image");
imageDelegate->pushImage(newImage);
tableWidget->repaint();
tableWidget->update();
tableWidget->setFocus();
}
#include "mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *widget = new QWidget;
setCentralWidget(widget);
tableWidget = new QTableWidget;
tableWidget->setColumnCount(7);
tableWidget->setRowCount(1);
imageDelegate = new Delegate(tableWidget);
tableWidget->setItemDelegate(imageDelegate);
tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
tableWidget->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
for (int i = 0; i < tableWidget->columnCount(); i++) {
tableWidget->setColumnWidth(i,400);
}
tableWidget->setRowHeight(0,400);
button = new QPushButton;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tableWidget);
layout->addWidget(button);
widget->setLayout(layout);
setMinimumSize(160,160);
resize(1000, 800);
QObject::connect(button, SIGNAL(clicked()), this, SLOT(add_image()));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(add_image()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
}
void MainWindow::add_image()
{
QImage newImage("/path/to/image");
imageDelegate->pushImage(newImage);
tableWidget->repaint();
tableWidget->update();
tableWidget->setFocus();
}
To copy to clipboard, switch view to plain text mode
Bookmarks