I'm making a board game and I need to display a 15 x 15 array in my gui. I decided to go with individual labels which contains one element of the array. This means I do have quite a lot of labels. I gave each label the name "tile_0_0" with the first 0 being the row and the second 0 being the column. This was quite easy to do in qt.
The problem however is that I ofcourse can't simply use 2 forloops to access each tile since you can't use variables in names. ("tile_i_j" does not exist.) To solve this I decided to make an array which contains each and every label. I however cannot initialise the array efficient because of the earlier mentioned problem.
So the question is: How can I avoid having to write a giant block of code?
A small piece of the current code:
Ui::BoardView *ui = new UI::BoardView; // BoardView is my class
QLabel* m_boardLbArray[8][8];
m_boardLbArray[0][0] = ui->tile_0_0;
m_boardLbArray[0][1] = ui->tile_0_1;
m_boardLbArray[0][2] = ui->tile_0_2;
m_boardLbArray[0][3] = ui->tile_0_3;
m_boardLbArray[0][4] = ui->tile_0_4;
// ...
Note: Sorry that I didn't post a part of code you could simply copy and run, but I do not know how since it's gui related.
It sounds like you are creating your tiles(QLabels) in Qt Designer; A cleaner way to achieve this is to create them programatically. You can do something like add a Grid Layout to your form in Designer in the location you want them, and then do:
QGridLayout *layout = ui->gridlayout;
QLabel* m_boardLbArray[8][8];
for(int row=0; row<8; row++)
for(int col=0; col<8; col++)
{
m_boardLbArray[row][col] = new QLabel(this);
m_boardLbArray[row][col]->setText(tr("This is row %1, col %2")
.arg(row).arg(col));
layout->addWidget(m_boardLbArray[row][col], row, col);
}
Related
I'm new to Qt. i want to cast a row item in the QTableWidget as object.
So far, i've manage to populate the QTableWidget with QList:
header.h
QList<Inventory> inventories;
int row = 0;
int rowCount = ui->tableItems->rowCount();
ui->tableItems->insertRow(rowCount);
foreach(Inventory inventory, this->inventories)
{
QTableWidgetItem *code = new QTableWidgetItem(inventory.getName());
QTableWidgetItem *name = new QTableWidgetItem(inventory.getCode());
QTableWidgetItem *price = new QTableWidgetItem(GlobalFunctions::doubleToMoney(this, inventory.getPrice()));
ui->tableItems->setItem(row,0,code);
ui->tableItems->setItem(row,1,name);
ui->tableItems->setItem(row,2,price);
row++;
}
In my table, i will select the row using this.
void CreateSalesWindow::removeItem()
{
qDebug() << "Remove Item" << ui->tableItems->currentIndex().column();
this->salesdetails.removeAt(ui->tableItems->currentIndex().column() - 1);
this->refreshItemList();
}
I've manage to get the selected row, is there a straightforward way to cast my row back into object. I've come from a C# .Net Background where i could easily cast it back into something like this (just an example). I could not found any good solutions in SO and Documentation.
Inventory selectedInventory = (Inventory) ui->tableItems->selectedItem().getValue();
qDebug() << selectedInventory.getPrice();
// 1699.75
PS. I also want to remove an item from the QList<> from the selected row in the table.
Thanks! I'm new to Qt, i'm open to much better approach in handling this. if something is unclear please raise a comment so i could correct it.
I'm not familiar with the QTableWidget itself, but in general you should use the row method with Qt's model/view classes to access the underlying data row index of the model and then just access your original data from your custom model (depending on the implementation of your model).
In your case something like this:
int rowIndex = ui->tableItems->selectedItems().first().row();
// or this should also work to get the current index directly
int rowIndex = ui->tableItems->currentIndex().row();
Inventory *selectedInventory = ui->tableItems->model()->getInventory(rowIndex);
where the getInventory(int index) method is your custom method to access your object via its index (I guess you have a derived model from QAbstractItemModel or something so save your data and view it in the QTableWidget).
That is at least what I would do, you can read more about general model/view programming with Qt at Introduction to Model/View Programming.
I need to search rows through a QTableWidget. Each of the rows in the table contains a field with date and I need to show only rows that are within a specified date interval based on user input. Here is my function:
void nvr::sort()
{
QTableWidget* tabela = this->findChild<QTableWidget*>("NCtable");
QDateEdit* c1 = this->findChild<QDateEdit*>("c1");
QDateEdit* c2 = this->findChild<QDateEdit*>("c2");
// user specified ranges for date
QDate date1 = c1->date();
QDate date2 = c2->date();
//row numbers in table
int rowsNum = tabela->rowCount();
// hide all rows
for(int z = 0; z < rowsNum; z++) {
tabela->hideRow(z);
}
// show only rows that are within range
for(int z = 0; z < rowsNum; z++) {
QDateTime dateTime = QDateTime::fromString(tabela->item(z,2)->text(),"dd.MM.yyyy hh:mm");
QDate date = dateTime.date();
//date compares
if ( (date1.operator <=(date)) && (date2.operator >=(date) ) ) {
tabela->showRow(z);
}
}
}
This works fine if i have 200 rows. But when i have 30 000 rows and i surely will, the gui freezes because i suppose the function executes very slow. Any suggestions for faster execution?
It is hard to reproduce your problem, but here is the approach I would take:
Create a custom class to store the data of one row, let's call it
DataRow.
Store those in a QVector<DataRow>, that you can sort by Date1 for example.
Loop through this QVector<DataRow> and find the elements that correspond to the criteria.
Add those DataRow to a class derived from QAbstractItemModel.
Show this model derived from QAbstractItemModel with a QTableView.
QTableWidget is heavyweight and was not really built for speed. It's really convenient to build something quickly with few elements though.
QTableView is the one you want, with a custom model inherited from QAbstractItemModel.
Then, when the user requests a new input, you could just wipe the model and restart the process. This is not optimal, but the user should not see the difference. Feel free to add more logic here to keep the good elements and only remove the bad ones.
About the GUI freezing, one way to always avoid that is to have the GUI thread separated from other worker threads. The QThread documentation is exhaustive and can help you set up something like this.
I want to have the ability to update over 100 labels, so I was going to put them in an array like this:
voltage_label_array[0] = this->ui->Voltage_0;
voltage_label_array[1] = this->ui->Voltage_1;
voltage_label_array[...] = this->ui->Voltage_2;
voltage_label_array[...n] = this->ui->Voltage_n;
and then have this method
void MainWindow::updateValue(int i, int voltage){
voltage_label_array[i]->setText(QString::number(voltage));
}
but having 100 lines to set this up seems like a bad idea. Is there a way I can initialize a QLabel array inside a for loop or something?
If you need to do this, something is horribly wrong with your design. But it is possible.
Assuming your labels are named Voltage_0 to Voltage_99:
for(int i = 0; i < 100; ++i) {
auto ptr = this->findChild<QLabel*>(QString("Voltage_%1").arg(i));
voltage_label_array[i] = ptr;
}
This "solution" uses Qt's runtime reflection and carries the expected performance penalties.
But if you need to display several similar values, look up QListWidget and similar classes.
I have a QVBoxLayout created through the drag and drop section.
Inside of it AT RUN TIME I insert some widgets with the command
ui->verticalLayout->insertWidget() //using appropriate options.
All widgets I insert are of the same type/class.
I would like to cycle through the inserted widgets in order to perform some actions over them.
I suppose it is really simple but can't seem to find out how...
thank you all!
You can use QLayout::itemAt() to loop on the items of the layout. Then use QLayoutItem::widget() to get the widget:
for(int i = 0; i < layout->count(); ++i)
{
do_something(
layout->itemAt(i)->widget()
);
}
Note that widget() may return a null pointer.
I create a QLineEdit,set a validator and put it on the table with this code:
ui->moneyTableWidget->setCellWidget(rowsNum, 1, newQLineEdit);
Then I've got another class for manipulating the table's data doing the sum of every value of a column. Here's the code:
int Calculator::calculatePricesSum(QTableWidget &moneyTableWidget){
double total = 0;
QWidget *tmpLineEdit;
QString *tmpString;
for(int row=0; row<moneyTableWidget.rowCount(); row++){
tmpLineEdit = (QLineEdit*)moneyTableWidget.cellWidget(row,1);
tmpString = tmpLineEdit.text();
total += tmpString->toDouble();
}
return total;
}
But the building fails with this error:
/home/testpec/src/nokia
QT/MoneyTracker-build-simulator/../MoneyTracker/calculator.cpp:11:
error: cannot convert ‘QLineEdit*’ to
‘QWidget*’ in assignment
Why this convertion error?
Another subquestion: passing the table as reference saves memory right? Could this be the problem? Im developing for a Nokia smartphone and I think passing the object by value is a waste of memory...(sorry if is a dumb question but I'm a little rusty with C++ and all the pointers stuff...)
When you declare your tmpLineEdit, you should be declaring it as a QLineEdit* instead of a QWidget*. Your loop grabs the widget, casts it to a QLineEdit* and then tries to put it back into a QWidget*. Also, I'd recommend using qobject_cast<QLineEdit*> (or dynamic_cast) so that you can ensure the cast succeeded.
int Calculator::calculatePricesSum(QTableWidget &moneyTableWidget){
double total = 0;
QLineEdit* tmpLineEdit;
QString tmpString;
for(int row=0; row < moneyTableWidget.rowCount(); row++)
{
tmpLineEdit = qobject_cast<QLineEdit*>(moneyTableWidget.cellWidget(row,1));
if(NULL == tmpLineEdit)
{
// Do something to indicate failure.
}
tmpString = tmpLineEdit->text();
total += tmpString.toDouble();
}
return total;
}
As for your second question, passing by reference is probably a good idea - I know some of the classes in Qt (QImage in particular) use reference counting and implicit sharing so that you can pass around by value without worrying about the implications of large copy operations, but I'm not sure if a QTableWidget is in that category as well.