Results 1 to 8 of 8

Thread: Multiple QListWidget Sorting Problem

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Multiple QListWidget Sorting Problem

    Basically I have 8 QListWidgets but only 1 of them is visable I use the other Widgets to keep track of my data
    I have a text file that has Food name (the visible QListWidget), calorie amount, protein amount....ect (for the other 7 that are not visible).
    I have it so when I read my text file that contains all the data it will put the values in the QListWidgets
    example:
    QListWidget1 reads in the text of "Banana" for row # 1.
    QListWidget2 reads in the text of "121" for row # 1 in widget #2.
    QListWidget3 reads in the text of "232" for row # 1 in widget # 3.
    ect....for the rest of the QListWidgets;
    I then turn the strings for QListWidgets 2-8 into ints so I can keep track of their values and add them together.
    everything works perfect as QListWidgetItem row 1 = row 1 for the other 7 QListWidgets

    but say I have another type of food in QListWIdget1 called "Apple" that is row # 2 and it has its values for the other QListWidgets in row2 in all of them.
    Everything works perfect.

    I put sorting on in QListWidget1 say Ascending order so Apple1 gets switched to row 1 and Banana to row 2.
    How do I switch the values of the other 7 QListWidgets to match the order?

    here is my code for header:
    Qt Code:
    1. #ifndef LISTWIDGETDIALOG_H
    2. #define LISTWIDGETDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QListWidgetItem>
    6.  
    7.  
    8.  
    9. class ListWidgetDialog : public QDialog
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. ListWidgetDialog();
    15. //~ListWidgetDialog();
    16.  
    17.  
    18.  
    19.  
    20.  
    21. private:
    22. QListWidget *listWidget;
    23. QListWidget *invisWidget[7];
    24.  
    25.  
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 
    and the code for my c++
    Qt Code:
    1. #include <QtGui>
    2. #include "listwidgetdialog.h"
    3. #include <iostream>
    4.  
    5. using namespace std;
    6. int value[7];
    7. int totalValue[7];
    8.  
    9.  
    10.  
    11. int row;
    12.  
    13.  
    14.  
    15.  
    16.  
    17. ListWidgetDialog::ListWidgetDialog()
    18. {
    19.  
    20. listWidget = new QListWidget;
    21. for(int i = 0; i<7;i++){
    22. invisWidget[i] = new QListWidget;
    23. }
    24.  
    25. QFile file(QDir::homePath() + "/caloriecounter.txt");
    26. if (file.open(QIODevice::ReadOnly)) {
    27. QTextStream in(&file);
    28. in.setCodec("UTF-8");
    29. while (!in.atEnd()) {
    30. listWidget->addItems(QString(in.readLine()).split('\n'));
    31. for(int i = 0; i<7; i++){
    32. invisWidget[i]->addItems(QString(in.readLine()).split('\n'));
    33. }
    34. in.readLine();
    35. }
    36. }
    37.  
    38. QGridLayout *layout = new QGridLayout;
    39. layout->addWidget(listWidget,25, 1);
    40. listWidget->sortItems(Qt::AscendingOrder);
    41. for(int i = 0; i<7; i++){
    42. invisWidget[i]->sortItems(Qt::AscendingOrder);
    43. }
    44. setLayout(layout);
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56. listWidget->setCurrentRow(0);
    57. connect(listWidget,SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
    58. this,SLOT(listWidgetItemClicked(QListWidgetItem*)));
    59.  
    60. selectedItem = listWidget->currentItem()->text();
    61. }
    62.  
    63. void ListWidgetDialog::listWidgetItemClicked(QListWidgetItem *item)
    64. {
    65. selectedItem = item->text();
    66. row = listWidget->currentRow();
    67. for(int i = 0; i<7; i++){
    68. invisWidget[i]->setCurrentRow(row);
    69. value[i] = invisWidget[i]->currentItem()->text().toInt();
    70. }
    71.  
    72. }
    To copy to clipboard, switch view to plain text mode 

    I tried to delete all the stuff that has nothing to do with this problem so it might look a little funky
    hopefully I didnt delete any important lines


    Added after 34 minutes:


    One more question ontop of this one sorry =/
    I have two c++ files,
    one has a few QStringLists and I need add new items to it from another c++ file how would I do this?
    I have this in my c++ file 1:
    Qt Code:
    1. QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
    To copy to clipboard, switch view to plain text mode 
    im using those to set items in a QTreeWidget and I would like to add new items via QDialog.
    here is the code I have in c++ file two:
    Qt Code:
    1. void ListWidgetDialog::okButton_pressed(){
    2. extern QStringList foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
    3. for(int i = 0; i<24;i++){
    4. foods << selectedFoods[i];
    5. }
    6. for(int i = 0; i<7; i++){
    7. QString totalValueStr[7] = QString("%1").arg(totalValue[i]);
    8. totalCalories << totalValueStr[i];
    9. totalProteins << totalValueStr[i];
    10. totalCarbs << totalValueStr[i];
    11. totalFats << totalValueStr[i];
    12. totalSugars << totalValueStr[i];
    13. totalSodiums << totalValueStr[i];
    14. totalFibers << totalValueStr[i];
    15. }
    16. ListWidgetDialog::close();
    17. }
    To copy to clipboard, switch view to plain text mode 

    and I get these errors:
    undefined reference to 'foods'
    undefined reference to 'totalCalories'
    ect...for them all
    Any suggestions would be greatly appreciated
    Last edited by giblit; 27th March 2013 at 05:37.

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

    Default Re: Multiple QListWidget Sorting Problem

    What you are doing not a good approach, you will have problems maintaining such code, the probelms just started.

    I suggest not to maintain multiple QListWidgets, instead maintain only one QListWidget, and implement a custom QListWidgetItem class, and just re-implemnt data() and setData() virtual functions to handle user defined data roles. Each of the custom data like, calorie amount, protein amount etc would have ItemDataRole as (Qt::UserRole + 0), (Qt::UserRole + 1)

    Edit:
    Wait I think QListWidgetItem already supports multiple user data roles internally, I have not tested/used them, just give it a try.
    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.

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Multiple QListWidget Sorting Problem

    none of these roles look like they would work though
    enum Qt::ItemDataRole

    Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

    The general purpose roles (and the associated types) are:

    Constant Value Description
    Qt:isplayRole 0 The key data to be rendered in the form of text. (QString)
    Qt:ecorationRole 1 The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap)
    Qt::EditRole 2 The data in a form suitable for editing in an editor. (QString)
    Qt::ToolTipRole 3 The data displayed in the item's tooltip. (QString)
    Qt::StatusTipRole 4 The data displayed in the status bar. (QString)
    Qt::WhatsThisRole 5 The data displayed for the item in "What's This?" mode. (QString)
    Qt::SizeHintRole 13 The size hint for the item that will be supplied to views. (QSize)
    Roles describing appearance and meta data (with associated types):

    Constant Value Description
    Qt::FontRole 6 The font used for items rendered with the default delegate. (QFont)
    Qt::TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt::AlignmentFlag)
    Qt::BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush)
    Qt::BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead.
    Qt::ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush)
    Qt::TextColorRole 9 This role is obsolete. Use ForegroundRole instead.
    Qt::CheckStateRole 10 This role is used to obtain the checked state of an item. (Qt::CheckState)
    Accessibility roles (with associated types):

    Constant Value Description
    Qt::AccessibleTextRole 11 The text to be used by accessibility extensions and plugins, such as screen readers. (QString)
    Qt::AccessibleDescriptionRole 12 A description of the item for accessibility purposes. (QString)
    User roles:

    Constant Value Description
    Qt::UserRole 32 The first role that can be used for application-specific purposes.
    or could I just set the data as a random role and then just access that without doing what the role says?

    EDIT: actually could it be the Qt::UserRole i'm a litle confused on what it means tho

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

    Default Re: Multiple QListWidget Sorting Problem

    Take a look at this example, try running it and see how it works. I am storing 5 user data with in a QListWidgetItem along with its default text.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Object : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit Object(QObject * parent)
    9. : QObject(parent)
    10. {
    11.  
    12. }
    13.  
    14. public slots:
    15. void showDetails(const QModelIndex &index)
    16. {
    17. QListWidget * list = new QListWidget;
    18.  
    19. list->addItem(new QListWidgetItem(index.data().toString()));
    20. list->addItem(new QListWidgetItem(index.data(Qt::UserRole + 0).toString()));
    21. list->addItem(new QListWidgetItem(index.data(Qt::UserRole + 1).toString()));
    22. list->addItem(new QListWidgetItem(index.data(Qt::UserRole + 2).toString()));
    23. list->addItem(new QListWidgetItem(index.data(Qt::UserRole + 3).toString()));
    24. list->addItem(new QListWidgetItem(index.data(Qt::UserRole + 4).toString()));
    25.  
    26. list->show();
    27. }
    28. };
    29.  
    30. void addItem(QListWidget * list, int num)
    31. {
    32. const QString name = QString("Item-%1").arg(num);
    33. const QString data0 = QString("Data-%1").arg(num);
    34. const QString data1 = QString("Data-%1").arg(num);
    35. const QString data2 = QString("Data-%1").arg(num);
    36. const QString data3 = QString("Data-%1").arg(num);
    37. const QString data4 = QString("Data-%1").arg(num);
    38.  
    39. QListWidgetItem * item = new QListWidgetItem(name);
    40.  
    41. item->setData(Qt::UserRole + 0, data0);
    42. item->setData(Qt::UserRole + 1, data1);
    43. item->setData(Qt::UserRole + 2, data2);
    44. item->setData(Qt::UserRole + 3, data3);
    45. item->setData(Qt::UserRole + 4, data4);
    46.  
    47. list->addItem(item);
    48. }
    49.  
    50.  
    51. int main(int argc, char *argv[])
    52. {
    53. QApplication app(argc, argv);
    54.  
    55. for(int i = 0; i < 5; i++)
    56. addItem(&w,i);
    57.  
    58. Object * object = new Object(&w);
    59. w.connect(&w, SIGNAL(doubleClicked(QModelIndex)), object, SLOT(showDetails(QModelIndex)));
    60. w.show();
    61.  
    62. return app.exec();
    63. }
    64.  
    65. #include "main.moc"
    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. The following user says thank you to Santosh Reddy for this useful post:

    giblit (27th March 2013)

  6. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Multiple QListWidget Sorting Problem

    Thanks alot now I see, I wasn't quite sure on the Qt::UserRole this is my first program im making =p
    and do you know what I could do to fix the second error I am having with the external variables?

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

    Default Re: Multiple QListWidget Sorting Problem

    Make sure the C++ file with variable declaration is included in the project (pro file)

    and

    Do clean build, it should solve the problem.
    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.

  8. #7
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Multiple QListWidget Sorting Problem

    it is in the pro file and I did a clean build still getting the undefined.

    I have the variable names(QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers in c++ file one
    then I have external (extern QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers in c++ file two
    I even tried putting the variables in c++ file one into the header and then including the header in c++ file two but then I get some more errors about multiple definitions when there is only one

    EDIT:
    I guess I had the extern in the wrong file I just switched that to c++ file one and it worked good thanks again
    Last edited by giblit; 27th March 2013 at 06:22.

  9. #8
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: Multiple QListWidget Sorting Problem

    what am I doing wrong?
    I made a test program before I tried adding it to my actual program here is the code that works perfectly:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. for(int i = 0; i<50; i++){
    7. QString itemName = QString("Item %1").arg(i);
    8. //cout << "ITEM NAME: " << itemName.toStdString() << endl;
    9. for(int j = 0; j<50; j++){
    10. QString dataName = QString("Data %1").arg(j);
    11. //cout << "DATA NAME: " << dataName.toStdString() << endl;
    12. QVariant itemData(dataName);
    13. item->setData(Qt::UserRole + j, itemData);
    14. //cout << "Item data " << j << ": " << item->data(Qt::UserRole + j).toString().toStdString() << endl;
    15. }
    16. item->setText(itemName);
    17. ui->listWidget->insertItem(i, item);
    18. //cout << "DATA NAMES?: " << dataName.toStdString() << endl;
    19. }
    20. }
    21. void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
    22. {
    23. int row = ui->listWidget->currentRow();
    24. cout << ui->listWidget->item(row)->text().toStdString() << endl;
    25. for(int i = 0; i < 50; i++){
    26. cout << ui->listWidget->item(row)->data(Qt::UserRole + i).toString().toStdString() << endl;
    27. }
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    this is a main window app and i was too lazy to add the stuff dynamically so I just threw a QListWidget.

    my code on my real project that isnt working is this:
    oh and it doesnt have any errors and runs fine..but if I try and click on an item...it just crashes almost instantly

    Qt Code:
    1. QFile file(QDir::homePath() + "/caloriecounter.txt");
    2. if (file.open(QIODevice::ReadOnly)) {
    3. QTextStream in(&file);
    4. in.setCodec("UTF-8");
    5. while (!in.atEnd()) {
    6. QString itemName = (in.readLine());
    7. for(int i = 0; i<7; i++){
    8. QString dataName = (in.readLine());
    9. QVariant itemData(dataName);
    10. item->setData(Qt::UserRole + i, itemData);
    11. //cout << "Item data " << i << ": " << item->data(Qt::UserRole + i).toString().toStdString() << endl;
    12. }
    13. item->setText(itemName);
    14. listWidget->insertItem(inNum,item);
    15. inNum++;
    16. in.readLine();
    17. //cout << "ITEM READS: " << itemName.toStdString() << endl;
    18.  
    19. }
    20. }
    21. file.close();
    22.  
    23.  
    24. void ListWidgetDialog::listWidgetItemClicked(QListWidgetItem *item)
    25. {
    26. selectedItem = item->text();
    27. row = listWidget->currentRow();
    28. for(int i = 0; i<7; i++){
    29. invisWidget[i]->setCurrentRow(row);
    30. value[i] = invisWidget[i]->currentItem()->text().toInt();
    31. }
    32. for(int i = 0; i < 24 ; i++){
    33. food[i] = textEdit[i]->find(selectedItem);
    34. QTextCursor c(textEdit[i]->textCursor());
    35. c.movePosition(QTextCursor::Start);
    36. textEdit[i]->setTextCursor(c);
    37. }
    38. cout << "ITEM NAME: " << listWidget->currentItem()->text().toStdString() << endl;
    39. for(int i = 0; i < 7; i++){
    40. cout << listWidget->item(row)->data(Qt::UserRole + i).toString().toStdString() << endl;
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions would be greatly appreciated


    Added after 9 minutes:


    Okay..sorry guys apparently I just had to remove the invis widget and it works im still not sure exactly why that was preventing it from loading. maybe its like santosh said having multiple listwidgets is a bad idea (i was going to remove but didnt get around to it)
    Last edited by giblit; 28th March 2013 at 02:07.

Similar Threads

  1. sorting qsqltable on multiple columns
    By dortegat in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2012, 03:09
  2. qlistwidget custome sorting
    By migel in forum Newbie
    Replies: 0
    Last Post: 1st September 2011, 11:55
  3. Replies: 0
    Last Post: 16th February 2011, 09:23
  4. Replies: 1
    Last Post: 7th May 2010, 15:01
  5. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05

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.