PDA

View Full Version : QListWidget



bismitapadhy
18th March 2010, 05:58
I created QListWIdget in one class, wand to add item to that list widget from another class.

class TestScriptGeneratorDialog - QListWidget *m_scriptWidget;

Class PresskeyDialog -

pTest = new TestScriptGeneratorDialog;
pTest->m_scriptWidget->addItem("sdss");

Add item is not working. HOw can we do it.

aamer4yu
18th March 2010, 06:39
What is not working ? What error are you getting ?

Also, did you create object of QListWidget ? like m_scriptWidget = new QListWidget... ??????

bismitapadhy
18th March 2010, 06:44
Yes i created object of QListwidget.

I am not getting any error. But itm is not adding to QListWidget.

Lykurg
18th March 2010, 06:52
Please send more code. Best would be a minimal compilable example reproducing your problem.

aamer4yu
18th March 2010, 06:54
do you see the listwidget ?
also try calling listWidget->update() after calling addItem, and check if it works..

bismitapadhy
18th March 2010, 06:59
It is not working, I am sending you some more code.

TestScriptGeneratorDialog::TestScriptGeneratorDial og(QWidget * parent): QDialog(parent)
{
createWindow();
}

void TestScriptGeneratorDialog::createWindow()
{

m_scriptWidget = new QListWidget;
m_scriptWidget->setMaximumWidth(140);
}

void TestScriptGeneratorDialog::onAdd()
{
QString keyToAdd = (m_commandWidget->currentItem())->text();

if(keyToAdd == "Presskey")
{
PressKeyDialog *pressKey = new PressKeyDialog(this);
if (pressKey)
{
pressKey->exec();
delete pressKey;
}
}
}

PressKeyDialog::PressKeyDialog(QWidget * parent): QDialog(parent)
{
QPushButton *okButton = new QPushButton(tr("Ok"));
QPushButton *closeButton = new QPushButton(tr("Cancel"));

connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(okButton, SIGNAL(clicked()), this, SLOT(onOk()));
}

void PressKeyDialog::onOk(void)
{
pTest = new TestScriptGeneratorDialog;
pTest->m_scriptWidget->addItem("sdss");
pTest->m_scriptWidget->update();
accept();
}

bismitapadhy
18th March 2010, 06:59
TestScriptGeneratorDialog::TestScriptGeneratorDial og(QWidget * parent): QDialog(parent)
{
createWindow();
}

void TestScriptGeneratorDialog::createWindow()
{

m_scriptWidget = new QListWidget;
m_scriptWidget->setMaximumWidth(140);
}

void TestScriptGeneratorDialog:nAdd()
{
QString keyToAdd = (m_commandWidget->currentItem())->text();

if(keyToAdd == "Presskey")
{
PressKeyDialog *pressKey = new PressKeyDialog(this);
if (pressKey)
{
pressKey->exec();
delete pressKey;
}
}
}

PressKeyDialog::PressKeyDialog(QWidget * parent): QDialog(parent)
{
QPushButton *okButton = new QPushButton(tr("Ok"));
QPushButton *closeButton = new QPushButton(tr("Cancel"));

connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(okButton, SIGNAL(clicked()), this, SLOT(onOk()));
}

void PressKeyDialog:nOk(void)
{
pTest = new TestScriptGeneratorDialog;
pTest->m_scriptWidget->addItem("sdss");
pTest->m_scriptWidget->update();
accept();
}

Lykurg
18th March 2010, 07:12
and how does your header look like? is m_scriptWidget public? And please use the code tags.

bismitapadhy
18th March 2010, 07:15
{
Q_OBJECT
public:
TestScriptGeneratorDialog(QWidget * parent = 0);

QListWidget * m_commandWidget;
QListWidget * m_scriptWidget;

private slots:
void onAdd();
void onRemove();
void onFinish();

private:
void createWindow();
};
class PressKeyDialog : public QDialog
{
Q_OBJECT
public:
PressKeyDialog(QWidget *parent = 0);

private slots:
void onOk();

private:
QLineEdit * m_pPressTime;
TestScriptGeneratorDialog * pTest;
};

aamer4yu
18th March 2010, 07:16
Did you try to debug the code ? What path does it follow when you click the Add button ?
Code seems ok, hard to trace from this code. As lykurg said, a minimal compilable example reproducing your problem would be fair
Also, instead of PressKeyDialog, you could have used QMessageBox::question

bismitapadhy
18th March 2010, 07:21
Did you try to debug the code ? What path does it follow when you click the Add button ?
Code seems ok, hard to trace from this code. As lykurg said, a minimal compilable example reproducing your problem would be fair
Also, instead of PressKeyDialog, you could have used QMessageBox::question
I am using another QDialog because, except OK, Cancel i have QCheckbox, QLineedit to add in that dialog.

Lykurg
18th March 2010, 07:29
void PressKeyDialog::onOk(void)
{
pTest = new TestScriptGeneratorDialog;
pTest->m_scriptWidget->addItem("sdss");
pTest->m_scriptWidget->update();
accept();
}

Ehm, where did you show pTest?

bismitapadhy
18th March 2010, 07:50
TestScriptGeneratorDialog * pTest;

PTest i have taken as a pointer of this class. What to show.

Lykurg
18th March 2010, 07:54
What to show. Your dialog? pTest->exec();

bismitapadhy
18th March 2010, 12:32
Your dialog? pTest->exec();

I attached one file.On selecting each item on the left side it will open a dialog and pressing Ok in that dialog some item will be added to the rightside listwidget.

Everytime if i will create a new instance of the QLIstwidget, adding itm and doing exec() it is adding in the beginning of the list.

To keep one instance of QListWidget for all the dialog i need to keep a global variable which is not a good programming style.

So please let me know any other solution is there for this?

bismitapadhy
22nd March 2010, 05:20
Your dialog? pTest->exec();Did you have any solution for this

faldzip
22nd March 2010, 11:03
Everytime if i will create a new instance of the QLIstwidget, adding itm and doing exec() it is adding in the beginning of the list.

Hmm... So every time you are adding item to different QListWidget? So after clicking OK (or what do you have there) 10times, you will have 10 QListWidgets? I don't think that this is what you really want...


To keep one instance of QListWidget for all the dialog i need to keep a global variable which is not a good programming style.

So pass the pointer to your QListWidget to this dialog.

bismitapadhy
22nd March 2010, 11:44
Hmm... So every time you are adding item to different QListWidget? So after clicking OK (or what do you have there) 10times, you will have 10 QListWidgets? I don't think that this is what you really want...

So pass the pointer to your QListWidget to this dialog.I already searched and didn't found the solution.