PDA

View Full Version : QList Question



giblit
3rd April 2013, 06:46
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.


int numbers[4][2] ={
{2, 4},
{3, 6},
{5, 10},
{8, 16}
};
for(int i = 0; i<4; i++){
for(int j = 0; j<2; j++){
cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
}
cout << endl;
}
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:


QList<int> number;
QList<QList<int> > numberList;
number << 2 << 3 << 5 << 8;
numberList << number;
number[0] += 2;
number[1] += 3;
number[2] += 5;
number[3] += 8;
numberList << number;

for(int i = 0; i<numberList.size(); i++){
for(int j = 0; j<number.size(); j++){
// cout << number[j] << endl;
cout << "numberList["<<i<<"]["<<j<<"]: " << numberList[i][j] << endl;
}
}

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-development/cpp/threads/38527/2d-vector

vector<vector<int> > numbers;
int k[4] = {2, 3, 5, 8};
for ( int i = 0; i < 4; i++ ) {
numbers.push_back (vector<int>());
for ( int j = 0; j < 2; j++ ){
numbers[i].push_back (k[i]*(j+1));
}
}
for ( int i = 0; i < 4; i++ ) {
for ( int j = 0; j < 2; j++ ){
cout << "numbers["<<i<<"]["<<j<<"]: "<< numbers[i][j] << " " << flush;
}
cout << endl;
}

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.

ChrisW67
3rd April 2013, 08:39
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.

giblit
4th April 2013, 02:41
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:


//header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QList>
#include <QGridLayout>
#include <QListWidget>


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private slots:
void button1pressed();
void button2pressed();
void button3pressed();

private:
QWidget *widget;
QList<QPushButton*> button2List;
QPushButton *button1, *button2, *button3;
QGridLayout *layout;
QListWidget *listWidget;
QListWidgetItem *item;
};

#endif // MAINWINDOW_H

//main c++
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

//mainwindow c++
#include "mainwindow.h"
#include <iostream>
using namespace std;

int total;
QList<int> num;
QList<QList<int> > numbers;
MainWindow::MainWindow()
{

listWidget = new QListWidget;
item = new QListWidgetItem;
widget = new QWidget;
button1 = new QPushButton;
button3 = new QPushButton;
layout = new QGridLayout;

for(int i = 0; i<7; i++){
QVariant itemData(QString("%1").arg(i));
item->setData(Qt::UserRole + i, itemData);
}

item->setText("Apple");
listWidget->addItem(item);
listWidget->setCurrentRow(0);
button1->setText("New");
button3->setText("Ok");

layout->addWidget(button1);
layout->addWidget(button3);
layout->addWidget(listWidget);
widget->setLayout(layout);
setCentralWidget(widget);

connect(button1,SIGNAL(clicked()),
this,SLOT(button1pressed()));
connect(button3,SIGNAL(clicked()),
this,SLOT(button3pressed()));
}
void MainWindow::button1pressed(){
total++;
button2 = new QPushButton;
button2->setText(QString("Add%1").arg(total-1));
layout->addWidget(button2);
connect(button2,SIGNAL(clicked()),
this,SLOT(button2pressed()));
button2List << button2;
for(int i = 0; i<7; i++){
num << int();
}
}
void MainWindow::button2pressed(){
for(int i = 0; i<button2List.size(); i++){
if(QObject::sender() == button2List[i]){
for(int j = 0; j<7; j++){
num[j+(7*i)] += listWidget->currentItem()->data(Qt::UserRole + j).toInt();
}
}
}
}
void MainWindow::button3pressed(){
for(int i = 0; i<total; i++){
numbers << QList<int>();
for(int j = 0; j<7; j++){
numbers[i] << num[j+(7*i)];
cout << "numbers["<<i<<"]["<<j<<"]: " << numbers[i][j] << " " << flush;
}
cout << endl;
}
numbers.clear();
}

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)