need help in getting a logic to work
Hi all,
I have this problem which I have been pondering and can't seem to find a solution to it. Therefore, I need some expertise in programming field for help.
I have a group of widget (1 label, 1 combo Box, 1 push button)
this group can populate to n arbitrary number of times base on user requirements.
event involved is just the push button clicked event but I will have to get the text field of both the label and combo box in order to produce the output I want.
the problem in the code below is all button will only display the last group items (label and combo box) but not their own group item which is not a surprise as the pointers will eventually point to the last set.
How can i associate the groups together? Any input would be good.
P.S. I initially though of array of pointers but it will kept my amount of group I can have. or waste some mem from unused pointers.
disp.h
Code:
...
private slots:
void go_clicked();
private:
void createPlace();
QWidget *createCellWidget
(group grp
);
mapinfo map; <-- struct
disp.cpp
Code:
...
void disp::createPlace()
{
int i = 0;
foreach (group grp, map.grp) {
layout->addWidget(createCellWidget(grp), i, 0);
i++;
}
layout->setRowStretch(i, 10);
itemWidget->setLayout(layout);
toolbox->setMinimumWidth(itemWidget->sizeHint().width());
toolbox->addItem(itemWidget,"Select Place");
}
QWidget *disp
::createCellWidget(group grp
) {
foreach (place p, grp.building)
combo->addItem(p.placename);
combo->setMaximumWidth(200);
btn->setText("Go");
btn->setMaximumWidth(50);
grpname->setText(grp.grpname);
layout->addWidget(grpname, 0, 0);
layout->addWidget(combo, 1, 0);
layout->addWidget(btn,1,1);
widget->setLayout(layout);
connect(btn, SIGNAL(clicked()), this, SLOT(go_clicked()));
return widget;
}
void disp::go_clicked()
{
//something with own group's combo text and label text
qDebug() << grpname->text() << " ----- " << combo->currentText(); //testing
}
Re: need help in getting a logic to work
It very well may be that I didn't understand your post.
But one way to do it is by creating a simple custom widget something like
Code:
{
Q_OBJECT
public:
InfoWidget(const group &grp); //in the implementations you set the member widgets in the layout as you did in createCellWidget()
~InfoWidget ();
public slots:
void go_clicked();
protected:
}
Code:
InfoWidget::InfoWidget(const group &grp)
{
foreach (place p, grp.building)
m_comBox.addItem(p.placename);
m_comBox->setMaximumWidth(200);
m_button.setText("Go");
m_button.setMaximumWidth(50);
m_label.setText(grp.grpname);
layout->addWidget(&m_label, 0, 0);
layout->addWidget(&m_combo, 1, 0);
layout->addWidget(&m_button,1,1);
setLayout(layout);
connect(&m_button, SIGNAL(clicked()), this, SLOT(go_clicked()));
}
now you can access all the elements in the slot:
Code:
InfoWidget::go_clicked()
{
//m_button, m_comBox, m_label are all members and can be accessed.
}
and then you can do something like:
Code:
QWidget *disp
::createCellWidget(group grp
) {
// QVector<InfoWidget*> m_infoWidgets; in the header
InfoWidget *pInfoWidget = new InfoWidget(group);
m_infoWidgets.push_back(pInfoWidget);
return infoWidget;
}
Please note that this is more like pseudo code, I did not test for compilation, it more to illustrate the idea I am suggesting.
Re: need help in getting a logic to work
thanks. it works. but I did not use QVector as there is a conversion problem. Instead I just use the customWidget as it is.
Re: need help in getting a logic to work
Quote:
QVector as there is a conversion problem.
There should not be such a problem.
Can you post the code that gives you that error?
The vector allows you to have multiples such InfoWidgets.
Also, what I forgot to mention in my previous post, you need to make sure you clear up (destroy) the allocated custom widgets in the destructor.