Reputation: 33
I'm trying to make a basic UI for taking in user input and displaying the output, using Qt. I just started working with UIs, so this might seem trivial. I have to take in 3 sets of inputs, where each set consists of 90 integer values. Think of each set as a 2d array of 30 by 3. I'm trying to see what the best way to handle this user input would be in Qt. I have used Qt to work on a .ui file and the link below is of a screenshot of what it should look like.
https://i.sstatic.net/KedP7.png
To be more specific, I have used the QTableWidget to create the table itself. My question now is how do I access user input from each cell of the table and store it into an array for that whole table?
Help will be appreciated.
Thanks!
Upvotes: 1
Views: 2836
Reputation: 4286
QTableWidget::item(int row, int column)
returns the item at position (row, column).
Row can be between 0 and QTableWidget::rowCount()
– 1, column between 0 and QTableWidget::columnCount()
– 1
QTableWidgetItem::text()
returns the items text.
Create a function, which reads alls items and add them to an array in a loop, connect the pressed-signal of the Enter-Button with this function.
If you only want to read updated items: the QTableWidget::itemChanged()
signal sends the item, which was updated. QTableWidget::indexFromItem()
returns the index of the item, so you can update your array.
Upvotes: 1
Reputation: 68
This might help. (I dont have the privilege to comment):
I use a help_index which basically numbers the elements from left to right. Everytime a cell is edited (return pressed) the next element is selected (the +1 in the help_index line). If the last index is reached i focus the OK button.
You might read data at:
Tab Function:
void MainWindow::on_tableWidget_cellChanged(int row, int column)
{
QString test_STR1 = ui->tableWidget->item(row,column)->text(); // is string
qDebug(QString("OnCell %1,%2:%3").arg(column).arg(row).arg(test_STR).toLatin1());
// EG int array[rows][columns];
// QList<QStringList<QString>> (equivalent to: QList<QList<QString>>)
int columncount , rowcount, help_index,nextRow, nextColimn;
columncount = ui->tableWidget->columnCount();
rowcount = ui->tableWidget->rowCount();
help_index = column + row*columncount +1;
if(help_index < columncount*rowcount)
{
nextRow = help_index / columncount;
nextColimn = help_index % columncount;
ui->tableWidget->setCurrentCell(nextRow,nextColimn);
}
else
{
ui->pushButton_OK->setFocus();
}
}
Ok_button:
void MainWindow::on_pushButton_OK_clicked()
{
for(int i=ui->tableWidget->columnCount()-1;i>=0;i--)
for(int j=ui->tableWidget->rowCount()-1;j>=0;j--)
{
QString test_STR2 = ui->tableWidget->item(j,i)->text();
qDebug(QString("OnOk %1,%2:%3").arg(i).arg(j).arg(test_STR).toLatin1());
}
}
Upvotes: 0