Results 1 to 3 of 3

Thread: QList Question

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

    Default Re: QList Question

    okay so I can make 2d arrays easy if I know how many items I am going to have to start with but in my main project they are dynamically created so I can not do that and I need to make a 2d QList which I can not figure out how to do I have been tyring for like an hour now.
    here is an example with just a normal 2d array when I know the given values.
    Qt Code:
    1. int numbers[4][2] ={
    2. {2, 4},
    3. {3, 6},
    4. {5, 10},
    5. {8, 16}
    6. };
    7. for(int i = 0; i<4; i++){
    8. for(int j = 0; j<2; j++){
    9. cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
    10. }
    11. cout << endl;
    12. }
    To copy to clipboard, switch view to plain text mode 
    the output is:
    numbers[0][0]: 2 numbers[0][1]: 4
    numbers[1][0]: 3 numbers[1][1]: 6
    numbers[2][0]: 5 numbers[2][1]: 10
    numbers[3][0]: 8 numbers[3][1]: 16
    now here is what I am trying to do which does not work:
    Qt Code:
    1. QList<int> number;
    2. QList<QList<int> > numberList;
    3. number << 2 << 3 << 5 << 8;
    4. numberList << number;
    5. number[0] += 2;
    6. number[1] += 3;
    7. number[2] += 5;
    8. number[3] += 8;
    9. numberList << number;
    10.  
    11. for(int i = 0; i<numberList.size(); i++){
    12. for(int j = 0; j<number.size(); j++){
    13. // cout << number[j] << endl;
    14. cout << "numberList["<<i<<"]["<<j<<"]: " << numberList[i][j] << endl;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    I tried several ways but just cant seem to figure out making a 2d QList of ints =/
    any suggestions/help/explainations would be greatly appreciated.
    thanks.


    Added after 46 minutes:


    okay so I got it working somewhat by using a vector but I do not understand the code 100% from -- http://www.daniweb.com/software-deve...8527/2d-vector
    Qt Code:
    1. vector<vector<int> > numbers;
    2. int k[4] = {2, 3, 5, 8};
    3. for ( int i = 0; i < 4; i++ ) {
    4. numbers.push_back (vector<int>());
    5. for ( int j = 0; j < 2; j++ ){
    6. numbers[i].push_back (k[i]*(j+1));
    7. }
    8. }
    9. for ( int i = 0; i < 4; i++ ) {
    10. for ( int j = 0; j < 2; j++ ){
    11. cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
    12. }
    13. cout << endl;
    14. }
    To copy to clipboard, switch view to plain text mode 
    the output is:
    numbers[0][0]: 2 numbers[0][1]: 4
    numbers[1][0]: 3 numbers[1][1]: 6
    numbers[2][0]: 5 numbers[2][1]: 10
    numbers[3][0]: 8 numbers[3][1]: 16
    I have not used vectors before and when I looked up the reference for "push_back" it says it is the same as append I believe. but how come I can not use numbers.append or numbers << instead of numbers.push_back? is there a difference? it doesnt really have any info on push_back here http://qt-project.org/doc/qt-4.8/qvector.html#push_back
    any info/advice on vectors would be appreciated.
    Last edited by giblit; 3rd April 2013 at 05:46.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QList Question

    now here is what I am trying to do which does not work:
    You should try to explain in what way it does not work.

    You are creating a QList of two lists, each containing four numbers.
    Your original array is a 'list' of four 'lists', each containing two numbers.

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

    Default Re: QList Question

    when I said it didn't work I meant it wasn't the format I was hoping it to be beacuse I had no idea what I was doing when I was trying to make it [4][2] lol its okay though I just figured out
    you could add lists to lists dynamically...here is the code that is working not sure if it's the best way to do it or not if you have any suggestions to improve it let me know.
    here is the code:
    Qt Code:
    1. //header
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include <QPushButton>
    7. #include <QList>
    8. #include <QGridLayout>
    9. #include <QListWidget>
    10.  
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow();
    18.  
    19. private slots:
    20. void button1pressed();
    21. void button2pressed();
    22. void button3pressed();
    23.  
    24. private:
    25. QWidget *widget;
    26. QList<QPushButton*> button2List;
    27. QPushButton *button1, *button2, *button3;
    28. QGridLayout *layout;
    29. QListWidget *listWidget;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    33.  
    34. //main c++
    35. #include <QtGui/QApplication>
    36. #include "mainwindow.h"
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QApplication a(argc, argv);
    41. MainWindow w;
    42. w.show();
    43.  
    44. return a.exec();
    45. }
    46.  
    47. //mainwindow c++
    48. #include "mainwindow.h"
    49. #include <iostream>
    50. using namespace std;
    51.  
    52. int total;
    53. QList<int> num;
    54. QList<QList<int> > numbers;
    55. MainWindow::MainWindow()
    56. {
    57.  
    58. listWidget = new QListWidget;
    59. item = new QListWidgetItem;
    60. widget = new QWidget;
    61. button1 = new QPushButton;
    62. button3 = new QPushButton;
    63. layout = new QGridLayout;
    64.  
    65. for(int i = 0; i<7; i++){
    66. QVariant itemData(QString("%1").arg(i));
    67. item->setData(Qt::UserRole + i, itemData);
    68. }
    69.  
    70. item->setText("Apple");
    71. listWidget->addItem(item);
    72. listWidget->setCurrentRow(0);
    73. button1->setText("New");
    74. button3->setText("Ok");
    75.  
    76. layout->addWidget(button1);
    77. layout->addWidget(button3);
    78. layout->addWidget(listWidget);
    79. widget->setLayout(layout);
    80. setCentralWidget(widget);
    81.  
    82. connect(button1,SIGNAL(clicked()),
    83. this,SLOT(button1pressed()));
    84. connect(button3,SIGNAL(clicked()),
    85. this,SLOT(button3pressed()));
    86. }
    87. void MainWindow::button1pressed(){
    88. total++;
    89. button2 = new QPushButton;
    90. button2->setText(QString("Add%1").arg(total-1));
    91. layout->addWidget(button2);
    92. connect(button2,SIGNAL(clicked()),
    93. this,SLOT(button2pressed()));
    94. button2List << button2;
    95. for(int i = 0; i<7; i++){
    96. num << int();
    97. }
    98. }
    99. void MainWindow::button2pressed(){
    100. for(int i = 0; i<button2List.size(); i++){
    101. if(QObject::sender() == button2List[i]){
    102. for(int j = 0; j<7; j++){
    103. num[j+(7*i)] += listWidget->currentItem()->data(Qt::UserRole + j).toInt();
    104. }
    105. }
    106. }
    107. }
    108. void MainWindow::button3pressed(){
    109. for(int i = 0; i<total; i++){
    110. numbers << QList<int>();
    111. for(int j = 0; j<7; j++){
    112. numbers[i] << num[j+(7*i)];
    113. cout << "numbers["<<i<<"]["<<j<<"]: " << numbers[i][j] << " " << flush;
    114. }
    115. cout << endl;
    116. }
    117. numbers.clear();
    118. }
    To copy to clipboard, switch view to plain text mode 

    this is just a demo of what I am going to be using it for basically I am letting the users create text fields dynamically then each item they add to it from the listwidget will have their 7 set data values( eg apple could have 10 cals, 20 grams of fat, 20 grams of sodium ect..) then it will keep track of the totals for each category(cals, fats, sodiums, ect...) in each textEdit(the time).
    so if they create 4 text fields(times) then there will be 28 total values I am keeping track of. (will also have a total for the whole day)

    (ps it's going to be a pain in the butt to figure out how to load in the items I am creating I might have to save like 0's in the txt file then if the file spots a 0 just ignore it or something because im loading the data into a QTreeView and if the user has two times for one date and 4 times for another date I cant just loop like i was before when there was set times for each date =p but it looks a lot nicer this way)

Similar Threads

  1. QList question
    By MarkoSan in forum Qt Programming
    Replies: 18
    Last Post: 17th February 2011, 10:57
  2. QList<QList > question
    By SubV in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 15:14
  3. QList question
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2008, 07:51
  4. QList question
    By aamer4yu in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2007, 13:43
  5. QList question
    By ln\ in forum Newbie
    Replies: 2
    Last Post: 19th December 2006, 13:01

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.