How can I access an element on a form from a function?
-
Hello everyone, i start to learn QT and met with a problem to access an element on a form.
For example i need to get access to listWidget (QListWidget)void Output(QSet<int> numberList) { QListWidget *listWidget = new QListWidget(); QSetIterator<int> iterator(numberList); while(iterator.hasNext()) { listWidget -> addItem(QString::number(iterator.next())); } delete listWidget; }
If possible then write some accessor methods, for example easy and the one that is best to use
-
Within the class code of the form/widget made by Designer you can generally access the individual widget through the
ui
pointer thus,ui->listWidget->addItem(...)
. In general, the widgets that make up a form are private members of the class and would not be exposed directly outside.Your example code could be a member function:
void FormClass::Output(const QSet<int>& numberList) { QSetIterator<int> iterator(numberList); while(iterator.hasNext()) { ui->listWidget -> addItem(QString::number(iterator.next())); } }
-
Within the class code of the form/widget made by Designer you can generally access the individual widget through the
ui
pointer thus,ui->listWidget->addItem(...)
. In general, the widgets that make up a form are private members of the class and would not be exposed directly outside.Your example code could be a member function:
void FormClass::Output(const QSet<int>& numberList) { QSetIterator<int> iterator(numberList); while(iterator.hasNext()) { ui->listWidget -> addItem(QString::number(iterator.next())); } }
@ChrisW67 Access as I understand it happens through "::"? And the function must be declared in a private form block?
-
@ChrisW67 Access as I understand it happens through "::"? And the function must be declared in a private form block?
@Rezistence
You really need to explain much better what you are really asking/wanting. For example, your sample code makes no sense: your function creates aQListWidget
locally, does not add it to any UI, puts some items in it, and then destroys it. It "achieves nothing". Consequently it's hard to understand what you really have in mind. Furthermore,Output()
is a free function, and as such would have no access to the UI anyway.@ChrisW67's code is rather different. It's a
FormClass
member method. It assumes you have created aQListWidget
in Designer. It does not create or destroy that widget. It accesses it through theui
pointer created when you process and compile a DesignerFormClass.ui
file. It would only work as-is if defined in theFormClass.cpp
file. -
@Rezistence
You really need to explain much better what you are really asking/wanting. For example, your sample code makes no sense: your function creates aQListWidget
locally, does not add it to any UI, puts some items in it, and then destroys it. It "achieves nothing". Consequently it's hard to understand what you really have in mind. Furthermore,Output()
is a free function, and as such would have no access to the UI anyway.@ChrisW67's code is rather different. It's a
FormClass
member method. It assumes you have created aQListWidget
in Designer. It does not create or destroy that widget. It accesses it through theui
pointer created when you process and compile a DesignerFormClass.ui
file. It would only work as-is if defined in theFormClass.cpp
file.@JonB
I just need to display the items with set which is passed as a parameter to a ListWidget which is already set via the designer on the form. ChrisW67 answer help me, my function my function just didn't see the ui pointer. -
@JonB
I just need to display the items with set which is passed as a parameter to a ListWidget which is already set via the designer on the form. ChrisW67 answer help me, my function my function just didn't see the ui pointer. -
@Rezistence
Only a a class member function where theui
is defined can see it, like @ChrisW67'sFormClass::Output()
, not your originalOutput()
.@JonB Yes I got it, thanks