PDA

View Full Version : inserting a row in a QTableWidget doesn t work!



adonis
28th April 2007, 10:08
Hi everybody,
i want to insert a row in QTableWidget with a buttonclick.





class CoordinateTable:public QTableWidget
{
Q_OBJECT
public:
CoordinateTable(QWidget *parent=0);

private slots :
void addrow();

};


CoordinateTable::CoordinateTable(QWidget *parent)
: QTableWidget(parent)
{ setColumnCount(2);
setRowCount(2);
};
void CoordinateTable::addrow()
{
int row = CoordinateTable::rowCount();
CoordinateTable:: insertRow(row);

};

In main


....
CoordinateTable *tableWidget = new CoordinateTable;
QObject::connect(okbutton, SIGNAL(clicked()),
tableWidget ,SLOT(addrow()));

okbutton is a QT4 QPushButton.
Nothing happens when i click the button and i dont understand why.:confused:

jacek
28th April 2007, 16:31
What happens if you add "qApp->aboutQt();" to CoordinateTable::addrow()?

adonis
28th April 2007, 17:55
Hi jacek,Thanks for your reply.
If you mean like that:

void CoordinateTable::addrow()
{
int row = CoordinateTable::rowCount();
CoordinateTable::insertRow(row);
qApp->aboutQt();
};
it doesnt work too!

jacek
28th April 2007, 17:58
it doesnt work too!
Does "doesn't work" mean that the application runs, but it doesn't show the "About Qt" window when you click the button?

adonis
28th April 2007, 18:00
Does "doesn't work" mean that the application runs, but it doesn't show the "About Qt" window when you click the button?


Yes, that´s what i mean!

jacek
28th April 2007, 18:20
Add "CONFIG += console" to your .pro file, re-run qmake and make and see whether there are no error messages on the console.

How do you initialize okbutton?

jpn
28th April 2007, 19:20
Is the class declaration in a header file and listed in .pro?

adonis
28th April 2007, 19:24
I don t see any errors on the console;
The initialization of the okbutton is:

QPushButton *okbutton=new QPushButton("Ok");

marcel
28th April 2007, 19:26
Do you add your button to a layout? Because I don't see creating it with a parent, and if you don't add it to a layout, I'm not sure any signals will work?

Does connect says anything in the debug console?

Regards

adonis
28th April 2007, 19:43
it works now!:) i ve included a .moc file(i don t really understand how this works) and now every thing is OK.

Thank you guys!

jacek
28th April 2007, 20:00
i ve included a .moc file(i don t really understand how this works) and now every thing is OK.
That .moc file, among other things, contains the code that implements the signals and slots mechanism for your class. If you won't link your application with it, signals and slots won't work for your class. To avoid such problems in future, you should use qmake, CMake or any other build system, that understands Qt needs.

adonis
28th April 2007, 20:31
Thanks jacek for the explanation.Actually ,i am using qmake.Should I use the moc file each time i want to define Signals or Slots in a class??

jacek
28th April 2007, 21:25
Actually ,i am using qmake.Should I use the moc file each time i want to define Signals or Slots in a class??
If you use qmake, then there are two things to remember:
you have to re-run qmake after you add or remove Q_OBJECT macro to a class definition,
if a class definition with Q_OBJECT is placed in a .cpp file instead of a header file, you have to add #include "filename.moc" at the end of that file and re-run qmake.