Results 1 to 4 of 4

Thread: Display QStrings in QListWidget

  1. #1
    Join Date
    Apr 2017
    Posts
    46
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Display QStrings in QListWidget

    I am using RHEL 6 and Qt4.8
    Also using QtCreator.

    I have an array of QStrings that I read in from a .csv file.

    I wish to display these in a ListWidget that I have inserted into a mainwindow panel.

    the data type that the ListWidget accepts is (const QString & label)

    What conversion do I need to perform on the QStrings in order to add them the ListWidget?

    for(int i = 0; i< 100; i++ ) {
    ListWidget->addItem(qstr_array[i]); //compiles and runs but nothing is displayed in the list widget and puts 100 blank lines in the widget.
    }


    for(int i = 0; i< 100; i++ ) {
    ListWidget->addItem("snow"); //compiles and runs and displays correctly in the list widget
    }


    Thank you for any help
    Last edited by emp1953; 2nd May 2019 at 21:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Display QStrings in QListWidget

    That would indicate that your array elements are empty strings.

    Btw: if you read your input into QStringList, you can simply use QListWidget::addItems()

    Cheers,
    _

  3. #3
    Join Date
    Apr 2017
    Posts
    46
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display QStrings in QListWidget

    Quote Originally Posted by anda_skoa View Post
    That would indicate that your array elements are empty strings.

    Btw: if you read your input into QStringList, you can simply use QListWidget::addItems()

    Cheers,
    _
    I do print out the array using qDebug() and all the elements contain the correct strings. I'll try your QStringList suggestion.

    Thank You

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,139
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Display QStrings in QListWidget

    I do print out the array using qDebug() and all the elements contain the correct strings.
    Are you sure you didn't accidentally define qstr_array as both a member variable of the class and as a local variable at the place where you read the strings from the file? If you defined it as a local variable, that "hides" the member variable and will be destroyed as soon as the read function exits, leaving you with a member variable with no content.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QListWidget item display to LineEdit
    By dinesh123 in forum Qt Programming
    Replies: 3
    Last Post: 28th August 2015, 07:25
  2. Can't declare more than 7 QStrings ?!?!?!
    By ruben.rodrigues in forum Newbie
    Replies: 8
    Last Post: 21st July 2010, 14:42
  3. Replies: 2
    Last Post: 19th February 2010, 12:58
  4. The problem with the display of QListWidget
    By whyisosad in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2008, 19:41
  5. QListWidget::addItem and display immediately
    By vlg789 in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2007, 15:30

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.