PDA

View Full Version : QListWidget, help adding new items.



JeremyRussell
6th April 2011, 21:02
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.




#include "primary.h"
#include "ui_primary.h"

Primary::Primary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Primary)
{
ui->setupUi(this);
}

QString newName;
QString newNumber;
QString details;

void Primary::on_addBtn_click()
{
newName = ui->nameText->text();
newNumber = ui->numberText->text();
details = ui->details->toPlainText();

ui->list->addItem(new QListWidgetItem(QString("%1 %2").arg(newName).arg(newNumber)));
}

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



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

Rhayader
6th April 2011, 21:39
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 ".

JeremyRussell
6th April 2011, 23:34
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.


#include "primary.h"
#include "ui_primary.h"

Primary::Primary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Primary)
{
ui->setupUi(this);
}

QString newName;
QString newNumber;
QString details;

void Primary::on_addBtn_clicked()
{
QString newName = ui->nameText->text();
QString newNumber = ui->numberText->text();
QString details = ui->details->toPlainText();

ui->list->addItem(new QListWidgetItem(QString("%1 %2").arg(newName).arg(newNumber)));
}

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


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.

stampede
7th April 2011, 00:21
Try connecting manually in constructor:


#include <QDebug>
...
Primary::Primary(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Primary)
{
ui->setupUi(this);
bool status = connect(ui->addBtn, SIGNAL(clicked()), this, SLOT(on_addBtn_clicked()));
qDebug() << "connection status:" <<status;
}

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 ?

JeremyRussell
7th April 2011, 00:54
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.