giblit
27th March 2013, 05:37
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:
#ifndef LISTWIDGETDIALOG_H
#define LISTWIDGETDIALOG_H
#include <QDialog>
#include <QListWidgetItem>
class QListWidget;
class QPushButton;
class ListWidgetDialog : public QDialog
{
Q_OBJECT
public:
ListWidgetDialog();
//~ListWidgetDialog();
private:
QListWidget *listWidget;
QListWidget *invisWidget[7];
};
#endif
and the code for my c++
#include <QtGui>
#include "listwidgetdialog.h"
#include <iostream>
using namespace std;
int value[7];
int totalValue[7];
int row;
ListWidgetDialog::ListWidgetDialog()
{
listWidget = new QListWidget;
for(int i = 0; i<7;i++){
invisWidget[i] = new QListWidget;
}
QFile file(QDir::homePath() + "/caloriecounter.txt");
if (file.open(QIODevice::ReadOnly)) {
QTextStream in(&file);
in.setCodec("UTF-8");
while (!in.atEnd()) {
listWidget->addItems(QString(in.readLine()).split('\n'));
for(int i = 0; i<7; i++){
invisWidget[i]->addItems(QString(in.readLine()).split('\n'));
}
in.readLine();
}
}
QGridLayout *layout = new QGridLayout;
layout->addWidget(listWidget,25, 1);
listWidget->sortItems(Qt::AscendingOrder);
for(int i = 0; i<7; i++){
invisWidget[i]->sortItems(Qt::AscendingOrder);
}
setLayout(layout);
listWidget->setCurrentRow(0);
connect(listWidget,SIGNAL(currentItemChanged(QList WidgetItem*,QListWidgetItem*)),
this,SLOT(listWidgetItemClicked(QListWidgetItem*)) );
selectedItem = listWidget->currentItem()->text();
}
void ListWidgetDialog::listWidgetItemClicked(QListWidge tItem *item)
{
selectedItem = item->text();
row = listWidget->currentRow();
for(int i = 0; i<7; i++){
invisWidget[i]->setCurrentRow(row);
value[i] = invisWidget[i]->currentItem()->text().toInt();
}
}
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:
QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
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:
void ListWidgetDialog::okButton_pressed(){
extern QStringList foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
for(int i = 0; i<24;i++){
foods << selectedFoods[i];
}
for(int i = 0; i<7; i++){
QString totalValueStr[7] = QString("%1").arg(totalValue[i]);
totalCalories << totalValueStr[i];
totalProteins << totalValueStr[i];
totalCarbs << totalValueStr[i];
totalFats << totalValueStr[i];
totalSugars << totalValueStr[i];
totalSodiums << totalValueStr[i];
totalFibers << totalValueStr[i];
}
ListWidgetDialog::close();
}
and I get these errors:
undefined reference to 'foods'
undefined reference to 'totalCalories'
ect...for them all
Any suggestions would be greatly appreciated
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:
#ifndef LISTWIDGETDIALOG_H
#define LISTWIDGETDIALOG_H
#include <QDialog>
#include <QListWidgetItem>
class QListWidget;
class QPushButton;
class ListWidgetDialog : public QDialog
{
Q_OBJECT
public:
ListWidgetDialog();
//~ListWidgetDialog();
private:
QListWidget *listWidget;
QListWidget *invisWidget[7];
};
#endif
and the code for my c++
#include <QtGui>
#include "listwidgetdialog.h"
#include <iostream>
using namespace std;
int value[7];
int totalValue[7];
int row;
ListWidgetDialog::ListWidgetDialog()
{
listWidget = new QListWidget;
for(int i = 0; i<7;i++){
invisWidget[i] = new QListWidget;
}
QFile file(QDir::homePath() + "/caloriecounter.txt");
if (file.open(QIODevice::ReadOnly)) {
QTextStream in(&file);
in.setCodec("UTF-8");
while (!in.atEnd()) {
listWidget->addItems(QString(in.readLine()).split('\n'));
for(int i = 0; i<7; i++){
invisWidget[i]->addItems(QString(in.readLine()).split('\n'));
}
in.readLine();
}
}
QGridLayout *layout = new QGridLayout;
layout->addWidget(listWidget,25, 1);
listWidget->sortItems(Qt::AscendingOrder);
for(int i = 0; i<7; i++){
invisWidget[i]->sortItems(Qt::AscendingOrder);
}
setLayout(layout);
listWidget->setCurrentRow(0);
connect(listWidget,SIGNAL(currentItemChanged(QList WidgetItem*,QListWidgetItem*)),
this,SLOT(listWidgetItemClicked(QListWidgetItem*)) );
selectedItem = listWidget->currentItem()->text();
}
void ListWidgetDialog::listWidgetItemClicked(QListWidge tItem *item)
{
selectedItem = item->text();
row = listWidget->currentRow();
for(int i = 0; i<7; i++){
invisWidget[i]->setCurrentRow(row);
value[i] = invisWidget[i]->currentItem()->text().toInt();
}
}
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:
QStringList dates, foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
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:
void ListWidgetDialog::okButton_pressed(){
extern QStringList foods, totalCalories, totalProteins, totalCarbs, totalFats, totalSugars, totalSodiums, totalFibers;
for(int i = 0; i<24;i++){
foods << selectedFoods[i];
}
for(int i = 0; i<7; i++){
QString totalValueStr[7] = QString("%1").arg(totalValue[i]);
totalCalories << totalValueStr[i];
totalProteins << totalValueStr[i];
totalCarbs << totalValueStr[i];
totalFats << totalValueStr[i];
totalSugars << totalValueStr[i];
totalSodiums << totalValueStr[i];
totalFibers << totalValueStr[i];
}
ListWidgetDialog::close();
}
and I get these errors:
undefined reference to 'foods'
undefined reference to 'totalCalories'
ect...for them all
Any suggestions would be greatly appreciated