Newbee is wired...
-
Hello there,
i have done a simple first Project with a slider and a spinbox like probably the other 99,9% of noobs.
One time I did it writing by it by Hand and then by doing the same with the creator.
There are two things i´m wondering.1.) In the Hand written code "nothing wokrs", if i set the Maximum and the Minimum of the slider and spinbox by using the setMax and setMin functions. If i do it by setRange(min, max) everything is fine.
@void main(int argc, char** argv)
{
int a;
QApplication app(argc, argv);
QWidget* win = new QWidget;
QVBoxLayout* layout = new QVBoxLayout(win);
win->setWindowTitle("Signal-Slot 2 Richtungen");
win->setGeometry(30,30,300,300);QSpinBox* spin = new QSpinBox(); spin->setGeometry(50,50,100,100); spin->setValue(22); spin->setRange(0,100); layout->addWidget(spin); QSlider* slider = new QSlider(Qt::Horizontal); slider->setRange(0,100); QObject::connect(spin, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(int)), spin, SLOT(setValue(int))); layout->addWidget(slider); win->show(); app.exec();@
2.)
I don´t understand what the creator did for me (but it works)
2.1) i think in the end there must be similar code generated compared to the Hand written Version? When i open the form.cpp it Looks like this
@Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}Form::~Form()
{
delete ui;
}@For example there is no "Show" member function. But the form inherits from Qwidget and the constructor is given an Argument of the type QWidget, but in the calling main.cpp there is no Argument and AFAIK, if i define an own constructor the stock one doesn´t exist anymore.
So my question is, where does all the stuff happen what i have written in the "Handversion"?
@void main(int argc, char *argv[])
{
QApplication app(argc, argv);
Form myForm;
myForm.show();
app.exec();
}@best regards!
-
Hi and welcome to devnet,
What do you mean by nothing works ? It's a bit vague.
setGeometry on the QSpinBox is useless since you put it a layout, the placement and sizing will be done for you.
Also be aware that you have a memory leak in your application since you don't delete win.
For more information, have a look at the "Using a Designer UI File in Your Application" chapter.
-
Hello,
by "Nothing works" i ment, that the spinbox doesn´t Change it´s value when i click the arrows and that the SIGNAL-SLOT Connections doesn´t work.
-
Did you check whether you had any warning on the console ?
-
I'm not sure how the project was setup but it compiles just fine for me (except for: unused variable 'a' warning). In the project configuration (.pro file) I set
QT += core widgets
in main.cpp I used the next includes:
#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QSpinBox>
#include <QSlider>
With no changes to your main it just works for me, including the signal-slot communication. -
The reason for the warning there is an unused variable "a", is because i try how to use the widget with the slider and spinbox to manipulate a variable i can then use.
Am i correct, that the right way is
-
click together a widget of my desire (my_Widget)
-
set up a class (my_Class) which inherits my_Widget
-
set up a Slot in my_Class for valueChanged Signal
-
set up a GetValue(); function in my_Class
... it´s Long way to tip a rary^^
-
-
Hi,
First of all, always first set ranges, then the working values for your spinbox. That might cause a problem when the range is still 0 to 1 and you want to set it to 22, that won't work! geometries are not used when working in layouts. Only when spacers are used and there is free space left.
If the creator did something that you want, but you do not get how, check out the constructor in the ui file that it generates! It will teach you more then we are able to share here.
When signal/slots are not working, there are two ways of finding out if the connection was made. First is to debug and read out the returned bool from the connect function. Also when run the application output window in creator will list warnings if connections could not be made.Then the last post?
What do you need to do??
In basic I would suggest to inherit QWidget in your designed class. (or QFrame etc etc) and yes implementing a GetValue() etc is always a good way for interfaeing with your class.
There is no need for you to overwrite the signals from the inherited class.
Or did I misunderstand your post??