PDA

View Full Version : QTableWidget



therealjag
23rd February 2006, 18:35
hey there i am trying to get values from my table entered by the user - but i am still having trouble getting the users data. my table is initially filled with all zero's but when the user enters their own number the only number that i get back is the original number which was zero. ive tried using teh following code to get a number from a particluar cell but i only get back zero?? any idea's....

int multiply = demands->item(2,1)->data(Qt::DisplayRole).toInt();

demands is the name of my table and i just want the number the user enters at (2,1) to be stored inside multiply.

jacek
23rd February 2006, 18:41
What does this output?
qDebug() << demands->item(2,1)->data(Qt::DisplayRole);
(you need to add #include <QtDebug> on the top of your file and "CONFIG += debug" to .pro file)

therealjag
23rd February 2006, 18:53
i tried adding in that line - where should the output from qDebug appear?? i put that line of code in the same function as where the table got created - is that the correct place to put that piece of code? or should i create a seperate function, like a slot or something??

jpn
23rd February 2006, 19:16
How about this:

QTableWidgetItem::text() (http://doc.trolltech.com/4.1/qtablewidgetitem.html#text)

therealjag
23rd February 2006, 19:46
i cant get qDebug to output anywhere - i dont see it...is it meant to appear onto the command prompt??

jacek
23rd February 2006, 19:47
i tried adding in that line - where should the output from qDebug appear??
On the console, if you use windows, you have to add "CONFIG += console" to your .pro file.


i put that line of code in the same function as where the table got created - is that the correct place to put that piece of code? or should i create a seperate function, like a slot or something??
Put it just above the "int multiply = ..." line.

therealjag
25th February 2006, 04:10
hey there i am still trying to obtain the users data from my QTableWidget. i tried using the following line

int multiply = demands->item(2,1)->data(Qt::DisplayRole).toInt();

but it only gives the data that was originally there before the user enters the new data and not afterwards...do i have to add an event filter of some kind so that i only record the input - once the user has clicked on a cell and modifed it???

wysota
25th February 2006, 09:42
How do you change the value in your model? If data() retrieves an "old" value, it means there is no "new" value. It looks like editing the item failed or was not completed. Could you please show the editing code?

jacek
25th February 2006, 15:14
Please, don't start more than one thread on the same problem.

therealjag
26th February 2006, 23:48
oh rite ok heres my code for that tab then which the table is inserted in:


DemandsTab:DemandsTab(const QFileInfo &fileInfo, QWidget *parent)
: QWidget(parent)
{


QLabel *testme = new QLabel(tr("Please enter the traffic demands associated with each link below test this out:") ) ;

QTableWidget *demands = new QTableWidget(this) ;

demands->setRowCount(10);
demands->setColumnCount(10);

int row = demands->rowCount();
int column = demands->columnCount();
array = new int{MAXPOINTS};


QTableWidgetItem *flagged = new QTableWidgetItem(" * ") ;
flagged->setFlags(flagged->flags()& ~Qt::ItemIsEditable);



for(int i =0; i <10; i++)
{
for(int j = 0; j<10; j++)
{
demands->setItem(j,i, new QTableWidgetItem("0"));
}
}

for(int k = 0; k<10;k++)
{
QTableWidgetItem *flagged = new QTableWidgetItem("*");
flagged->setFlags(flagged->flags()& ~Qt:ItemIsEditable);
demands->setItem(k,k,flagged);
demands->horizontalHeader()->setResizeMode(k,HeaderView:Stretch) ;

}

/*int multiply = demands->item(2,1)->data(Qt::DisplayRole).toInt();
qDebug("Value of the number is %d", multiply);*/

//int item = QVariant(QTableWidgetItem::data(int multiply));
//item->setData(int multiply, int QVariant::toint(bool *ok = 0));
//demands->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);

qDebug()<<demands->item(2,1)->data(Qt::DisplayRole);


QTableWidgetItem *flagers = new QTableWidgetItem({"23")};
demands->setItem(2,1,flagers);

array[0] = demands->item(2,1)->data(Qt::DisplayRole).toInt();


QDataStream & operator<<(QDataStream & out, const QTableWidgetItem & flagers);



QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(testme) ;
mainLayout->addWidget(demands) ;
setLayout(mainLayout);
}

i have a seperate function where i output the number for outputTable( ) basically its just this:

display->setText(QString::number(array[0]));

jacek
26th February 2006, 23:59
display->setText(QString::number(array[0]));
Where do you change that array[0]? Only in constructor?

PS. Please, use the code tags to make your code readable.

therealjag
27th February 2006, 01:42
yeah only in the demandsTab function. the array is a global array....i kno i shouldnt do it but its the only way i can seem to get the data over into the other function.

jacek
27th February 2006, 01:57
yeah only in the demandsTab function. the array is a global array....
But if you change it only in the constructor, it will have always the same value.


i kno i shouldnt do it but its the only way i can seem to get the data over into the other function.
You can use signals instead.

therealjag
27th February 2006, 02:32
could you give me an example please of how a signal is used?

jacek
27th February 2006, 02:36
Start from here: http://doc.trolltech.com/4.1/tutorial-t1.html

therealjag
27th February 2006, 13:58
ahh ok thanks - ur help is much apreciated

therealjag
27th February 2006, 19:04
hey there i was trying to use the cellClicked signal bu the debug mode error message says that cellClicked() SIGNAL doesnt exist. i am also trying to pass in my table but it again says the SLOT doesnt exist??

my code for the connect is below:


connect(demands,SIGNAL(cellClicked(2, 1)),this,SLOT(obtain_values(QTableWidget *demands)));



the demands table is declared in the demandsTab constructor and the obtain_values function is shown below:


void DemandsTab::obtain_values(QTableWidget *demands)
{

QTableWidgetItem *flagers = new QTableWidgetItem(("23"));
demands->setItem(1,2,flagers);
array[0] = demands->item(2,1)->data(Qt::DisplayRole).toInt();

char test = 'j';
qDebug()<<test;

}


i dont know why it doesnt recognise the table and the signal - any idea's?

jacek
27th February 2006, 20:05
connect(demands,SIGNAL(cellClicked(2, 1)),this,SLOT(obtain_values(QTableWidget *demands)));
no parameter values are allowed in SIGNAL and SLOT macros,
no parameter names are allowed in SIGNAL and SLOT macros,
signal and slot signatures must match.

Try:
connect( demands, SIGNAL( cellClicked( int, int ) ), this, SLOT( obtain_values( int, int ) ) );
and
void DemandsTab::obtain_values( int row, int col )
{
// ...
QTableWidget *item = demands->item( row, col );
if( item != 0 ) {
array[0] = ->data(Qt::DisplayRole).toInt();
}
char test = 'j';
qDebug()<<test;
}
Make sure that obtain_values() is declared as slot and that you have Q_OBJECT macro within class definition.

therealjag
28th February 2006, 02:28
i ran the code and it compiled ok but the debugger still says no such signal "cellClicked(int, int)" do i have to put this signal into my header file or something like that, because i tried it before and it still didnt work?

jacek
28th February 2006, 14:42
Could you post the exact error message?

therealjag
28th February 2006, 16:39
the error message from the debugger is attached with this reply....

jacek
28th February 2006, 16:42
This signal was introduced in Qt 4.1, but you are using Qt 4.0 --- that's why it couldn't be found.

therealjag
28th February 2006, 17:28
hey thanks for pointing that out - i never realised. i used itemClicked instead and it seems to be working. thanks for your help again!