PDA

View Full Version : How can I dynamically create a new widgets next to the other widget?



Hossein
20th October 2015, 08:15
Hello everyone,
I am trying to create a button which is next to a slider,or a spinbox (or any other widgets) when this button is clicked a textbox widgets would popup and user could enter his values in it (which then affects the silder)
In order to get this done, I declared a QLineEdit object with its parent set to null, so that I can show up alone, next to the slider which it is going to change its value.
Now I am stuck where how I am supposed to set the qlinedits location.


auto loc =ui->toolButton->geometry();
spinbox->setGeometry(loc.x(),loc.y(),loc.width()+10,loc.hei ght()+10);
spinbox->setWindowTitle("change your preference");
ui->txtInfo->append(QString::number(loc.x())+" "+QString::number(loc.y()));
spinbox->move(this->geometry().topLeft().rx()+loc.x(),this->geometry().topLeft().ry()+loc.y());
spinbox->show();

Here I am trying to pop up a spinbox near the toolbutton,none of these work!
I appreciate any help
Thanks in advance

Radek
20th October 2015, 09:21
See QStackedWidget. The widget will contain 2 pages. The first page is a placeholder (a QWidget), the second page is the QLineEdit. Initially, you will show the first page (nothing will be seen). When you click the button, you switch to the second page and the line edit will pop up. When you finish editing, you can set the slider and switch to the first page again.
The stacked widget is in the Designer menu so that you can create the UI in the Designer.

Hossein
20th October 2015, 11:28
See QStackedWidget. The widget will contain 2 pages. The first page is a placeholder (a QWidget), the second page is the QLineEdit. Initially, you will show the first page (nothing will be seen). When you click the button, you switch to the second page and the line edit will pop up. When you finish editing, you can set the slider and switch to the first page again.
The stacked widget is in the Designer menu so that you can create the UI in the Designer.

Thanks, but I'd like to find out how to get the current logic to work.
I'd definitely give QStackedWidget a try, but before that I might not like it, it may not look well in my current GUI setup, so I am still looking how to get around the problem.
thank you again, I appreciate your kind answer.:)

Added after 1 20 minutes:

Ok got it. here it is

auto frmloc =this->geometry();
auto tBtnLocation =ui->toolButton->geometry();
spinbox->setWindowTitle("change your prefrence");
spinbox->move(frmloc.x()+tBtnLocation.x()+150,
frmloc.y()+tBtnLocation.y()+80);
spinbox->show();
ui->txtInfo->append(QString::number(spinbox->value()));
I forgot about the parents location. I had to take that into consideration and now it is done:)

Hossein
22nd October 2015, 13:43
Thanks, but I'd like to find out how to get the current logic to work.
I'd definitely give QStackedWidget a try, but before that I might not like it, it may not look well in my current GUI setup, so I am still looking how to get around the problem.
thank you again, I appreciate your kind answer.:)

Added after 1 20 minutes:

Ok got it. here it is

auto frmloc =this->geometry();
auto tBtnLocation =ui->toolButton->geometry();
spinbox->setWindowTitle("change your prefrence");
spinbox->move(frmloc.x()+tBtnLocation.x()+150,
frmloc.y()+tBtnLocation.y()+80);
spinbox->show();
ui->txtInfo->append(QString::number(spinbox->value()));
I forgot about the parents location. I had to take that into consideration and now it is done:)
Thats wrong, its only relative to the parent container.
the working solution is here :
http://www.qtcentre.org/threads/18333-Absolute-widget-coordinates