Results 1 to 7 of 7

Thread: QList Problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QList Problem

    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

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: QList Problem

    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
    Qt Code:
    1. ui->somebutton->setText(list[0]);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QList Problem

    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(...).

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: QList Problem

    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.
    Qt Code:
    1. QList<QLabel*> labels;
    2. QList<QString> list;
    3.  
    4. list << "lb1" << "lb2" << "lb3";
    5.  
    6. for(int i = 0; i < list.size(); < i++)
    7. {
    8. QLabel * lbl = new QLabel(); /// \todo take care of widget parent, or put the widget in a layout properly.
    9. labels.append(lbl);
    10. }
    11.  
    12. for(int i = 0; i < labels.size(); < i++)
    13. {
    14. labels[i]->setText(lbl[i]);
    15. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: QList Problem

    I think QListWidget 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:
    Qt Code:
    1. #include <QApplication>
    2. #include <QListWidget>
    3. #include <QHBoxLayout>
    4. #include <QListWidget>
    5.  
    6. int main(int argc, char** argv)
    7. {
    8. QApplication a(argc,argv);
    9. QHBoxLayout* layout = new QHBoxLayout(&w);
    10.  
    11. //QListWidget is a convenience class
    12. //usefull to quickly show a list and you don't need to complicate your life with MV
    13. QListWidget* listWidget = new QListWidget();
    14.  
    15. // alternative for QList<QString>,
    16. // is actually derived from QList<QString>
    17. // and it implements some methods that might be usefull - example join
    18. QStringList listOfStrings;
    19.  
    20. //add some items into the QStringList
    21. //here i use the operator<<, but it has other methods like append... see the documentation
    22. listOfStrings << "item 0" << "item 1" << "item 2";
    23.  
    24. //add the items from the QStringList
    25. listWidget->addItems(listOfStrings);
    26.  
    27.  
    28. layout->addWidget(listWidget);
    29. w.show();
    30. return a.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

    Further reading Model/View

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: QList Problem

    A list widget is a totally different thing than a list of labels, especially UI-wise.

    Cheers,
    _

  7. #7
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList Problem

    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

Similar Threads

  1. Problem with QList
    By just-in in forum Qt Programming
    Replies: 9
    Last Post: 22nd August 2013, 18:12
  2. problem with QList
    By yamen ajjour in forum Newbie
    Replies: 4
    Last Post: 22nd August 2011, 08:17
  3. Replies: 4
    Last Post: 20th August 2010, 13:54
  4. QList problem
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 19th January 2010, 05:40
  5. QList problem
    By acix in forum General Programming
    Replies: 6
    Last Post: 29th April 2006, 13:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.