Results 1 to 10 of 10

Thread: Problem with QList

  1. #1
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Problem with QList

    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).

    Qt Code:
    1. QList<Card*>* newcard = new QList<Card*>;
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. ui->textlabel->setText(newcard->takeAt(0)->getfront());
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. void MyDialog::on_pushbutton_clicked()
    2. {
    3. ui->textlabel->setText(newcard->takeAt(0)->getback());
    4. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QMessageBox::information(this,"Title",QString::number(newcard->count()));
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by just-in; 22nd August 2013 at 11:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QList

    Why do you have a list of Card pointers and not a list of Card objects?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    just-in (22nd August 2013)

  4. #3
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QList

    If im using a list of Card objects instead of pointers I get the following error message:

    Qt Code:
    1. 'QObject& QObject::operator=(const QObject&)' is private
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. #ifndef CARD_H
    2. #define CARD_H
    3.  
    4. #include <QObject>
    5.  
    6. class Card : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit Card(void);
    11. QString book;
    12. QString category;
    13. QString chapter;
    14. bool known;
    15. bool important;
    16. QString title;
    17. QString front;
    18. QString rueck;
    19. int id;
    20.  
    21.  
    22.  
    23. QString getbook();
    24. QString getcategory();
    25. QString getchapter();
    26. bool getknown();
    27. bool getimportant();
    28. QString gettitle();
    29. QString getvorder();
    30. QString getrueck();
    31.  
    32. signals:
    33.  
    34. public slots:
    35. void setbook(QString);
    36. void setcategory(QString);
    37. void setchapter(QString);
    38. void setknown(bool);
    39. void setimportant(bool);
    40. void settitle(QString);
    41. void setfront(QString);
    42. void setback(QString);
    43.  
    44. };
    45.  
    46. #endif // CARD_H
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QList

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    just-in (22nd August 2013)

  7. #5
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QList

    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.

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include <QMessageBox>
    4.  
    5. Dialog::Dialog(QWidget *parent) :
    6. QDialog(parent),
    7. ui(new Ui::Dialog)
    8. {
    9. ui->setupUi(this);
    10. QList<QString>* list = new QList<QString>;
    11. list->append(QString("ST"));
    12. QMessageBox::information(this,"T",list->at(0));
    13. }
    14.  
    15. Dialog::~Dialog()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void Dialog::on_pushButton_clicked()
    21. {
    22. QMessageBox::information(this,"T",list->at(0)); // this leads to the SIGSEGV
    23. }
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QList

    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>;
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. The following user says thank you to high_flyer for this useful post:

    just-in (22nd August 2013)

  10. #7
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QList

    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.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QList

    Better yet don't use a pointer to QList but rather a QList object directly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #9
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QList

    Quote Originally Posted by wysota View Post
    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?

  13. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with QList

    Quote Originally Posted by just-in View Post
    What is the advantage here of using the object directly than using a pointer?
    No crashes
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    just-in (22nd August 2013)

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. QList problem
    By frenk_castle in forum Newbie
    Replies: 1
    Last Post: 19th January 2010, 05:40
  3. Problem with QList
    By Gargolissimus in forum Newbie
    Replies: 8
    Last Post: 6th July 2009, 16:24
  4. QList problem
    By lvi in forum Qt Programming
    Replies: 3
    Last Post: 25th August 2008, 18:22
  5. QList problem
    By acix in forum General Programming
    Replies: 6
    Last Post: 29th April 2006, 13:08

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.