PDA

View Full Version : QList Problem



xxxollixxx
10th December 2013, 15:35
Hi Guys,

I've got a little problem with the QList function. The plan is to create a list of Label names, f.ex. lb1,lb2,lb3,...
This works with QList<QString> list; list<<"lb1"<<"lb2"<<"lb3"...;

and now I want to use the list entry's for setting an label.

Instead of ui->lb1->setText("..."); I want to use ui->list[0]->setText("...");

But the last code gives me an error(class Ui::Mainwindow has no member named list)

I guess I cant use the QList<QString> on Widgets?

best regards,

olli

Radek
10th December 2013, 16:02
list[0] is a QString (not a pointer).

(a) Most likely, the string is not a member of your UI
(b) A QString has no setText() method.

Therefore


ui->somebutton->setText(list[0]);

deusinvictus
10th December 2013, 16:48
If I'm understanding correctly, you have several labels in a widget with various names, lbl1, lbl2 etc. And you want to create a a QList<QString> (or you could use QStringList) with the variable names and access them using that? I'm not sure of how you could do something like that in C++ off the top of my head. list[0] would return the Qstring with the value "lbl1", but that is a string, not a pointer or reference to the lbl1 QLabel in the class. The line ui->list[0]->setText(...) is looking for a list member inside the ui class which doesn't exist. I'm pretty sure I understand what you're trying to do, but I can't think of any way at the moment to do it in C++. What you would need to do would create a QList<QLabel*> and add pointers to those labels in the list. Then you could just do list[0]->setText(...).

Santosh Reddy
10th December 2013, 16:58
Instead of ui->lb1->setText("..."); I want to use ui->list[0]->setText("...");
Qt designer does not support creating list of labels. If you want to assign text using a for loop, then better create the labels in the code (instead of using the Qt Designer). Here is an example.


QList<QLabel*> labels;
QList<QString> list;

list << "lb1" << "lb2" << "lb3";

for(int i = 0; i < list.size(); < i++)
{
QLabel * lbl = new QLabel(); /// \todo take care of widget parent, or put the widget in a layout properly.
labels.append(lbl);
}

for(int i = 0; i < labels.size(); < i++)
{
labels[i]->setText(lbl[i]);
}

Zlatomir
10th December 2013, 21:05
I think QListWidget (https://qt-project.org/doc/qt-5.1/qtwidgets/qlistwidget.html#details) class is a better solution instead of a QList<QLabel*> and it doesn't complicate the code that much.

Nevertheless, because it seems to me you are a beginner in C++, i have some advice: learn C++ before Qt, at least until you know OOP in C++ and you have a pretty good understanding of pointers and memory management, else Qt might seem very "unfriendly", and it's not like that Qt is very easy to use, but because it's a C++ framework it requires some C++ knowledge.

Anyway here is a little example code that uses QListWidget:


#include <QApplication>
#include <QListWidget>
#include <QHBoxLayout>
#include <QListWidget>

int main(int argc, char** argv)
{
QApplication a(argc,argv);
QWidget w;
QHBoxLayout* layout = new QHBoxLayout(&w);

//QListWidget is a convenience class
//usefull to quickly show a list and you don't need to complicate your life with MV
QListWidget* listWidget = new QListWidget();

// alternative for QList<QString>,
// is actually derived from QList<QString>
// and it implements some methods that might be usefull - example join
QStringList listOfStrings;

//add some items into the QStringList
//here i use the operator<<, but it has other methods like append... see the documentation
listOfStrings << "item 0" << "item 1" << "item 2";

//add the items from the QStringList
listWidget->addItems(listOfStrings);


layout->addWidget(listWidget);
w.show();
return a.exec();
}


Further reading Model/View (https://qt-project.org/doc/qt-5.1/qtwidgets/model-view-programming.html)

anda_skoa
11th December 2013, 10:31
A list widget is a totally different thing than a list of labels, especially UI-wise.

Cheers,
_

xxxollixxx
11th December 2013, 10:47
Thanks for your answers! I'll have to think about my idea. Creating some dynamic label list is maybe not the best solution...anyway thanks a lot!

Best regards,

Olli