Results 1 to 5 of 5

Thread: QListWidget, help adding new items.

  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QListWidget, help adding new items.

    Hi I'm new here, used to program a lot (visual basic) and decided to jump into QT on my return to programming.

    Now here's my problem. I'm pretty sure I followed all the steps correctly but upon compiling(which it does) and then running. I click my button and... nothing. No list getting filled and no crashing either, not sure were I went wrong but any help would be great.

    Qt Code:
    1. #include "primary.h"
    2. #include "ui_primary.h"
    3.  
    4. Primary::Primary(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::Primary)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. QString newName;
    12. QString newNumber;
    13. QString details;
    14.  
    15. void Primary::on_addBtn_click()
    16. {
    17. newName = ui->nameText->text();
    18. newNumber = ui->numberText->text();
    19. details = ui->details->toPlainText();
    20.  
    21. ui->list->addItem(new QListWidgetItem(QString("%1 %2").arg(newName).arg(newNumber)));
    22. }
    23.  
    24. Primary::~Primary()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

    If anyone has any ideas I'd be much obliged. It's probably just something dumb I'm missing but I don't know.

  2. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget, help adding new items.

    Your code should work so the only thing I can think is that maybe you changed the button's name from "addBtn" to something else in designer and the on_addBtn_click slot isn't called. In this case your code will compile but you should had a runtime warning like "QMetaObject::connectSlotsByName: No matching signal ".

  3. #3
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget, help adding new items.

    I changed click to clicked because that's what it's supposed to be. Then added QString in front of the code for pulling data out of the line edits. Still nothing though.

    Qt Code:
    1. #include "primary.h"
    2. #include "ui_primary.h"
    3.  
    4. Primary::Primary(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::Primary)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. QString newName;
    12. QString newNumber;
    13. QString details;
    14.  
    15. void Primary::on_addBtn_clicked()
    16. {
    17. QString newName = ui->nameText->text();
    18. QString newNumber = ui->numberText->text();
    19. QString details = ui->details->toPlainText();
    20.  
    21. ui->list->addItem(new QListWidgetItem(QString("%1 %2").arg(newName).arg(newNumber)));
    22. }
    23.  
    24. Primary::~Primary()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Also checked my ui_primary.h (equivalent to the ui_mainWindow.h file if you choose the default class name). the QPushButton is named "addBtn" so I think that's fine.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QListWidget, help adding new items.

    Try connecting manually in constructor:
    Qt Code:
    1. #include <QDebug>
    2. ...
    3. Primary::Primary(QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::Primary)
    6. {
    7. ui->setupUi(this);
    8. bool status = connect(ui->addBtn, SIGNAL(clicked()), this, SLOT(on_addBtn_clicked()));
    9. qDebug() << "connection status:" <<status;
    10. }
    To copy to clipboard, switch view to plain text mode 

    I think you don't need QStrings in lines 11-13, you have local strings in slot with the same name. Maybe it's just a typo ?

  5. The following user says thank you to stampede for this useful post:

    JeremyRussell (7th April 2011)

  6. #5
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget, help adding new items.

    Nah that's just how it is in another program I wrote.

    On that note I fixed it. The debug script stuff told me that there was no slot, so I looked at my header and it I had the void on_addBtn_clicked(); line of code in the public: section, not the public slots: section. The program now works(kind of, it adds the stuff twice for some reason but I'll tinker with it for a bit before asking for help. Thankyou for your assistance.

    EDIT: I fixed the double add bug as well. Turns out having those extra three lines causes the slot to get a signal twice in a row instead of once.

Similar Threads

  1. Replies: 2
    Last Post: 1st April 2011, 10:32
  2. Replies: 1
    Last Post: 22nd February 2010, 11:53
  3. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 11:03
  4. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 21:04
  5. Getting all items of a QListWidget
    By Codepoet in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2006, 23:52

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.