PDA

View Full Version : QTableWidget



martial_arts_drummer
28th December 2010, 15:04
Hi,
First, let me explain, I am an old timer coder, but have never dealt with QT.

My question is:
I created a QTableWidget using the gui creator (.ui) I then want to load data
into the 2 dim. array.

I attempted associated the slot "on_tablewidget_activated" and used
tableWidget->setItem to load the data, but when debugging, it never
enters the function on_tablewidget_activated.

How do I load data into the TableWidget?

Snippet of code:

********************************
.h file:

class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget *parent = 0);
QTableWidget tableWidget(QWidget *parent = 0);
~Widget();

protected:
void changeEvent(QEvent *e);

private:
Ui::Widget *ui;

private slots:
void on_tableWidget_activated(QModelIndex index);

*******************************************

.cpp file
#include "widget.h"
#include "ui_widget.h"

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

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

void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

void Widget::on_tableWidget_activated(QModelIndex index)
{
QTableWidgetItem *item = new QTableWidgetItem(QString("testing"));
tableWidget->setItem(1,1,item);

}


thanks

John

high_flyer
28th December 2010, 15:15
Are you sure you want to use the activated() signal?
The slot will be called when you activate the item which has the index 'index', which is not what I think you want.
Just loading data to the table can be done via a normal method call (for example in the constructor).

martial_arts_drummer
28th December 2010, 15:43
Hi,
Thanks for the info. I tried moving it to the constructor and am getting the following syntax error. Can you see what I am doing wrong?

/home/john/qt/projects/perf1/widget.cpp:10: error: invalid use of member (did you forget the ‘&’ ?)

h file:

#include <QModelIndex>
#include <QTableWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget *parent = 0);
QTableWidget *tableWidget(QWidget *parent = 0);
~Widget();

protected:
void changeEvent(QEvent *e);

private:
Ui::Widget *ui;

private slots:
void on_tableWidget_activated(QModelIndex index);
};

#endif // WIDGET_H

c file:
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QTableWidgetItem *item = new QTableWidgetItem(QString("testing"));
tableWidget->setItem(1,1,item);
}

high_flyer
28th December 2010, 16:15
you probably meant

tableWidget()->setItem(1,1,item);

martial_arts_drummer
28th December 2010, 16:18
thanks. that did the trick.

it was one of those "I can't see the forest through the trees"