Beginners : how to input parameter to a widget
-
Hi guys I want to create a widget and send parameter input_filename from main to it but i don't know how
@
#include <QtGui/QApplication>
#include "widget.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;return a.exec();
}
@@
Widget::Widget(QWidget *parent, QString input_filename = "setting.dB") :
QWidget(parent)
{
}
@ -
Just pass your input into Widget's Constructor (this is C++ knowledge, not Qt knowledge)
@
// In Qt convention, we put 'parent' last so that it can always accept a default value (0)
Widget::Widget(QString input_filename = "setting.dB", QWidget *parent = 0) :
QWidget(parent)
{
}int main(int argc, char *argv[])
{
QApplication a(argc, argv);// Default input ("setting.dB"), no parent (null pointer) Widget w1(); // Custom input ("my_input.dB"), no parent (null pointer) Widget w2("my_input.dB"); // Custom input ("my_input.dB"), custom parent (pointer to other widget) QWidget *other_widget = /* Create other widget */ Widget w3("my_input.dB", other_widget); // Show all three widgets w1.show(); w2.show(); w3.show(); return a.exec();
}
@ -
Is it possible to return a value after it the widget ends?
something like
QString path="xxxx";
return path;should I call it this way
QString v;
v=Widget w("file1");thanks in advance
-
[quote author="SherifOmran" date="1346316853"]Is it possible to return a value after it the widget ends?
something like
QString path="xxxx";
return path;should I call it this way
QString v;
v=Widget w("file1");thanks in advance[/quote]
No, constructors didn't have return type...
Using constructor for expensive calculations is always bad idea....Use constructor for initializations only... set all needed values, create needed instances.
Create another one function like job() where you will make needed job, this function can then return value... Show your widget in this function or do whatever you want.... -
SherifOmran, I suggest you follow this tutorial: http://doc.trolltech.com/4.3/tutorial.html It's good for learning how to use Qt widgets.
Do you have experience in C++?
-
I am learning by doing my friend
-
[quote author="SherifOmran" date="1346324303"]I am learning by doing my friend[/quote]Then I wish you all the best. :) Keep reading, coding, and asking! I recommend the link I posted before -- it's how I learnt Qt.
What other programming languages do you know? It's helpful to find resources that match your experience, and it will help you learn faster.
-
I know VB :)
-
C++ is quite different than VB. It's a lot more powerful too.
I tried to find a "C++ tutorial for VB programmers", but couldn't find one, sorry :(