PDA

View Full Version : QTableWidget



dragon
17th April 2007, 18:21
Hello anyone,

I ám using fc6 with Qt-4.2
I have a Dialog with a freesTable widget with 1 collum.
I can add a row through a another Dialog into freesTable no problem.
I want to hold the row in a QList<> and when i startup the application it will iterate first
through the QList and place the row in my freesTable.
But there is nothing showing up in freesTable.
Can anybody help me with this problem.

The header file frees.h


#include <QtGui>
#include "ui_freesdialog.h"

class freesDialog : public QDialog, public Ui::freesDialog
{
Q_OBJECT

public:
freesDialog(QList<QString> *list, QWidget *parent=0);

public slots:
void add();

private slots:
void done(int result);

private:
QList<QString> list;

};


The implentation file frees.cpp


#include <QtCore>

#include "frees.h"
#include "frezen.h"

freesDialog::freesDialog(QList<QString> *list, QWidget *parent)
:QDialog(parent)

{
setupUi(this);

for(int row = 0; row < list->count(); ++row) {
QString frees = list->at(row);
freesTable->setItem(row, 0, new QTableWidgetItem(frees)); //Something is wrong here.
}
connect(pbAdd, SIGNAL(clicked()), this, SLOT(add()));
connect(pbRemove, SIGNAL(clicked()), this, SLOT(remove()));
}


void freesDialog::add()

{
frezenDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {

QString materiaal = dlg.matComboBox->currentText();
int row = freesTable->rowCount();
freesTable->insertRow(row);
freesTable->setItem(row, 0, new QTableWidgetItem(materiaal));
}
}


void freesDialog::done(int result)
{

if(result == QDialog::Accepted) {
for(int row = 0; row < freesTable->rowCount(); ++row) {
QString materiaal = freesTable->item(row, 0)->text();
list.append(QString(materiaal)); // Give a error Undefined reference to Frezen::Frezen(QString const&)
}
}
QDialog::done(result);
}


The main file main.cpp


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

int main(int argc, char *argv[])

{
QApplication app(argc, argv);
QList<Frezen> list;
freesDialog freesdlg(&list);
return freesdlg.exec();
}



Thanks in advance.

jpn
17th April 2007, 18:25
Maybe you forgot to set the column count? See QTableWidget::setColumnCount() or do it via designer.

marcel
17th April 2007, 18:27
Actually its more likely you forgot setRowCount()...

dragon
17th April 2007, 19:08
I have set the collumcount in QT Designer i think tht is not the problem.
I think the problem sits in the frees.cpp in the line's.

#
for(int row = 0; row < list->count(); ++row) {
#
QString frees = list->at(row);
#
freesTable->setItem(row, 0, new QTableWidgetItem(frees)); //Something is wrong here.
#
}

and in the line's
#
for(int row = 0; row < freesTable->rowCount(); ++row) {
#
QString materiaal = freesTable->item(row, 0)->text();
#
list.append(QString(materiaal)); // Give a error Undefined reference to Frezen::Frezen(QString const&)

marcel
17th April 2007, 19:14
Where do you populate the list you pass in main.cpp to the dialog? Ibelieve there is no problem, its just the list... It is empty...

dragon
17th April 2007, 19:45
I populate the list in frees.cpp


void freesDialog::done(int result)
{

if(result == QDialog::Accepted) {
for(int row = 0; row < freesTable->rowCount(); ++row) {
QString materiaal = freesTable->item(row, 0)->text();
list.append(QString(materiaal)); // Give a error Undefined reference to Frezen::Frezen(QString const&)
}
}
QDialog::done(result);
}


When compiling he give's a error on line
list.append(QString(materiaal));
I think there some wrong in the code on this line.

marcel
17th April 2007, 20:08
I don't understand something: in main.cpp you create the dialog by passing a QList to it.
In the constructor of the dialog you fill a table from this list ( WHICH IS EMPTY AT THE MOMENT !!! ). What do you expect to see? Or, is this the correct code? Maybe I'm missing something.

dragon
17th April 2007, 20:34
Maybe i must change the main.cpp because the code is not right.


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

int main(int argc, char *argv[])

{
QApplication app(argc, argv);
QList<Frezen> list;
freesDialog freesdlg(&list);
return freesdlg.exec();
}


I will delete the line
QList<Frezen> list;
Mabye that will solve the problem.

dragon
17th April 2007, 20:46
I have delete the line QLIst<Frezen> list.


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

int main(int argc, char *argv[])

{
QApplication app(argc, argv);
freesDialog freesdlg;
return freesdlg.exec();
}



When i compile the application it give's a error.
main.cpp error: no matching function for call to freesDialog::freesDialog().

marcel
17th April 2007, 20:51
because freesDialog does not have a default constructor ( a constructor with no parameters ).

From what I've seen the constructor of freesDialog has two parameters: a QList and a QWidget.

You should modify the constructor or create it accordingly in main.cpp.

Regards.

dragon
17th April 2007, 21:52
Can you give me a example over modify the constructor

marcel
17th April 2007, 21:57
Sure...

In the header:


public:
freesDialog( QWidget* parent = NULL );
...


In the cpp:


freesDialog::freesDialog( QWidget* parent )
:QDialog( parent )
{
... do some stuff
}


Now, when you create the dialog, you can call the constructor without any parameters.
Also, there is no point in filling the table from that empty list in the constructor... So you can remove it...


Regards

dragon
18th April 2007, 17:55
It is not working the Qlist stays empty i think.
Nothing showing up in QTableWidget.
I try to search for orther sollution.

Thanks Marcel for your time and suggestions.

marcel
18th April 2007, 18:55
Definitely you're doing something wrong there... What is in the list and where the list gets populated? I mean, where do you put items in the list?

Before filling the table from the list, there has to be actually something in the list in the first place....

Regards.

dragon
18th April 2007, 20:15
I know something wrong there...

I get my items from this with some comment on the line's maybe helps.



void freesDialog::add()

{
frezenDialog dlg(this); // When i clicked on de pushbutton it shows this inputdialog.
if( dlg.exec() == QDialog::Accepted ) {

QString materiaal = dlg.matComboBox->currentText(); // the text i choose in my matComboBox
int row = freesTable->rowCount();
freesTable->insertRow(row);
freesTable->setItem(row, 0, new QTableWidgetItem(materiaal)); //Put the text onto the QTableWidget.

}
}


From another function
void freesDialog::done(int result)


QString materiaal = freesTable->item(row, 0)->text();
list.append(materiaal); //materiaal must append to QList<Qstring> list


I think this the method to put something in list maby i wrong.