PDA

View Full Version : Problem with QList



just-in
22nd August 2013, 12:17
Hello everyone,

I'm relatively new to Qt and to programming as a whole. So be clement ;)

My system is Windows 7, I am using Qt 5.1 with MinGW 32bit.

My problem:

I have a dialog that reads data from an sqlite database and writes it in a QList object. This QList contains objects of a C++ class I created (the class is called 'Card' and contains eight different QStrings).


QList<Card*>* newcard = new QList<Card*>;

I have one label on my dialog that should display a QString from the first object of my QList.


ui->textlabel->setText(newcard->takeAt(0)->getfront());

This works fine, the label is correctly displaying the QString. But then I want to change the text of the label when clicking a pushbutton.


void MyDialog::on_pushbutton_clicked()
{
ui->textlabel->setText(newcard->takeAt(0)->getback());
}


And this produces a SIGSEGV and the program crashes. I tried to fix the problem with different approaches, but none of them worked. The following code works neither:


QMessageBox::information(this,"Title",QString::number(newcard->count()));

I also cannot transmit the QList to another dialog and work with it there. It crashes as well.


I think this problem has a simple solution and I am just to much of an amateur to see that. Please help me!

Well this thread should probably be in the newbie section, but I don't know how to move the thread...sorry.

wysota
22nd August 2013, 12:49
Why do you have a list of Card pointers and not a list of Card objects?

just-in
22nd August 2013, 14:05
If im using a list of Card objects instead of pointers I get the following error message:


'QObject& QObject::operator=(const QObject&)' is private

Here is the header file of my Card class. Maybe this helps:


#ifndef CARD_H
#define CARD_H

#include <QObject>

class Card : public QObject
{
Q_OBJECT
public:
explicit Card(void);
explicit Card(QString,QString,QString,QString,QString, QString, QString, QString, int);
explicit Card(QString,QString,QString,QString,QString, QString, QString, QString);
QString book;
QString category;
QString chapter;
bool known;
bool important;
QString title;
QString front;
QString rueck;
int id;



QString getbook();
QString getcategory();
QString getchapter();
bool getknown();
bool getimportant();
QString gettitle();
QString getvorder();
QString getrueck();

signals:

public slots:
void setbook(QString);
void setcategory(QString);
void setchapter(QString);
void setknown(bool);
void setimportant(bool);
void settitle(QString);
void setfront(QString);
void setback(QString);

};

#endif // CARD_H

wysota
22nd August 2013, 15:39
Why does your class inherit QObject? All your members are public so having getters and setters for them does not make any sense.

By the way, takeAt() removes the item from the list.

just-in
22nd August 2013, 17:38
Thanks for the reply. I told you this should have been moved to the Newbie section :)

But the problem remains. I also tried to create a QList<QString> object in the constructor of my dialog and then call in in the on_pushbutton_clicked() SLOT.

I get the error message "This application has requested the Runtime to terminate it in an unsual way..." and in the output of the application: "Invalid parameter passed to C runtime function."

I reproduced the error in a simple Test dialog. Please check if you can find my mistake. I really appreciate your effort.


#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QList<QString>* list = new QList<QString>;
list->append(QString("ST"));
QMessageBox::information(this,"T",list->at(0));
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::on_pushButton_clicked()
{
QMessageBox::information(this,"T",list->at(0)); // this leads to the SIGSEGV
}

high_flyer
22nd August 2013, 17:58
I don't know if this is your only problem but it is A problem:
You define a QList pointer in your constructor which "shadows" the one you defined in the header file.
So the pointer you are initializing in the constructor is not the one your are calling on in your slot which is why the application crashes.
Remove the definition of the pointer in the constructor - just do:
list = new QList<QString>;

just-in
22nd August 2013, 18:13
Thank you very much. That solved my problem, at least this one....
I am just trying to learn some programming in my freetime so it's nice to have this support.

wysota
22nd August 2013, 18:30
Better yet don't use a pointer to QList but rather a QList object directly.

just-in
22nd August 2013, 18:46
Better yet don't use a pointer to QList but rather a QList object directly.

Alright. I also changed the property settings in my Card-class (that was definitely a beginner's mistake). Works fine now.

What is the advantage here of using the object directly than using a pointer?

wysota
22nd August 2013, 19:12
What is the advantage here of using the object directly than using a pointer?

No crashes :)