Setting position of a widget
-
i want to set the position of a widget on top of another widget dynamically. how do i do it. what is the syntax can anyone help please. i want the new widget to be placed at exactly the same position where another is placed but on top of it
-
Hi,
You can do this- Get the position of the bottom widget
- Using these coordinates set the position for the new top widget
For E.g:
@
QLabel *bottom = new QLabel(this);
bottom->setGeometry(10,10,200,200);QLabel *top = new QLabel(this);
top->setGeometry(bottom->x(),bottom->y(),200,200);// or
top->move(bottom->x(),bottom->y());
@ -
i tried that but the size of the widgets are not the same so it goes more down.
i want the the bottom point of the widgets to be the same and not the top .
so the bottom point of the widget should match with the bottom of the first one -
Are the two widgets of the same size ?
-
no the first one is much smaller the width is same but the the height of the one to be positioned is much more
-
Then you need to do some calculations.
Try this and see if this is what you want@
QLabel *bottom = new QLabel(this);
bottom->setGeometry(250,250,200,50);
bottom->setStyleSheet("background-color: rgb(255,0,0)");QLabel *top = new QLabel(this);
top->resize(200,200);
top->move(bottom->x(),bottom->y()-(top->height()-bottom->height()));
top->setStyleSheet("background-color: rgba(0,0,255,150)");
@I have set the alpha component of the top widget so that we may see the effect.
-
it is appearing at that position but not on top of it i want it to appear on top of the previous widget but on doing this the previous widget just changes its position and appears over or under it .
-
This is because i have set the alpha component of the top widget to make it appear transparent so that we may see the bottom points of the two widgets coincide.
Change
@top->setStyleSheet("background-color: rgba(0,0,255,150)");@
to
@top->setStyleSheet("background-color: rgb(0,0,255)");@and check if this is what you want
-
no i meant that the transparency is not happening it first component is moving its position they appear like this
list1
list2
while i want that they appear over one another
i want the second widget to be placed over every other widget like a menu is visible over the others -
Can you post you code or what you have tried? It would be hard to guess what would be the problem in your code.
-
list1 = new QListWidget;
list1->addItem("tamnmay");
list1->addItem("hghj");
list1->addItem("tamnmay");
list1->addItem("hghj");
list1->addItem("tamnmay");
list1->addItem("hghj");
list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);for(int i=0;i<list1->count()-1;i++) { list1->setRowHidden(i,true); } list1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Minimum); list1->setMaximumWidth(1500); list1->installEventFilter(this); setMinimumSize(700, 500); list1->setMaximumHeight(20); setWindowState(Qt::WindowMaximized); b1->setMaximumWidth(50); b1->setFlat(true); layout->addWidget(l1); layout->addWidget(b1); //layout->addWidget(list1); widget->setLayout(layout); widget->setStyleSheet("background-color: white; margin:0px"); QDockWidget *dock = new QDockWidget; dock->setFeatures(QDockWidget::NoDockWidgetFeatures); dock->setAllowedAreas(Qt::BottomDockWidgetArea ); customerList = new QListWidget(); customerList->addItems(QStringList() << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton" << "Jane Doe, Memorabilia, 23 Watersedge, Beaton" << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton" << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal" << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston" << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula"); customerList->setWindowFlags(Qt::Popup); customerList->setVisible(false); QWidget *widget2 = new QWidget; QVBoxLayout *lay = new QVBoxLayout; //lay->addWidget(customerList); lay->addWidget(list1); widget2->setLayout(lay);
QWidget* lTitleBar = dock->titleBarWidget();
QWidget* lEmptyWidget = new QWidget();
lEmptyWidget->setVisible(false);statusBar()->addWidget(l1,0);
dock->setTitleBarWidget(lEmptyWidget);
delete lTitleBar;
dock->setWidget(widget2);
addDockWidget(Qt::BottomDockWidgetArea, dock);}
bool MainWindow::eventFilter(QObject* object, QEvent* event )
{if (object==list1) { if(event->type()==QEvent::Enter) { for(int i=1;i<list1->count();i++) { list1->setRowHidden(i,false); } //list1->setVisible(false); //customerList->setVisible(true); // customerList->show(); customerList->setGeometry(list1->x(),list1->y(),300,500); menu=new QMenu(this); menu->exec(list1->pos()); //list1->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); //list1->setMaximumHeight(60); } }
-
Please can you include it in Code tags. Its difficult to understand like this.
-
sorry
@list1 = new QListWidget;
list1->addItem("tamnmay");
list1->addItem("hghj");
list1->addItem("tamnmay");
list1->addItem("hghj");
list1->addItem("tamnmay");
list1->addItem("hghj");
list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);for(int i=0;i<list1->count()-1;i++) { list1->setRowHidden(i,true); } list1->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Minimum); list1->setMaximumWidth(1500); list1->installEventFilter(this); setMinimumSize(700, 500); list1->setMaximumHeight(20); setWindowState(Qt::WindowMaximized); b1->setMaximumWidth(50); b1->setFlat(true); layout->addWidget(l1); layout->addWidget(b1); //layout->addWidget(list1); widget->setLayout(layout); widget->setStyleSheet("background-color: white; margin:0px"); QDockWidget *dock = new QDockWidget; dock->setFeatures(QDockWidget::NoDockWidgetFeatures); dock->setAllowedAreas(Qt::BottomDockWidgetArea ); customerList = new QListWidget(); customerList->addItems(QStringList() << "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton" << "Jane Doe, Memorabilia, 23 Watersedge, Beaton" << "Tammy Shea, Tiblanka, 38 Sea Views, Carlton" << "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal" << "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston" << "Sally Hobart, Tiroli Tea, 67 Long River, Fedula"); customerList->setWindowFlags(Qt::Popup); customerList->setVisible(false); QWidget *widget2 = new QWidget; QVBoxLayout *lay = new QVBoxLayout; //lay->addWidget(customerList); lay->addWidget(list1); widget2->setLayout(lay);
QWidget* lTitleBar = dock->titleBarWidget();
QWidget* lEmptyWidget = new QWidget();
lEmptyWidget->setVisible(false);statusBar()->addWidget(l1,0);
dock->setTitleBarWidget(lEmptyWidget);
delete lTitleBar;
dock->setWidget(widget2);addDockWidget(Qt::BottomDockWidgetArea, dock);
}
bool MainWindow::eventFilter(QObject* object, QEvent* event )
{if (object==list1) { if(event->type()==QEvent::Enter) { for(int i=1;i<list1->count();i++) { list1->setRowHidden(i,false); } //list1->setVisible(false); //customerList->setVisible(true); customerList->show(); customerList->setGeometry(list1->x(),list1->y(),300,500); //customerList->show(); menu=new QMenu(this); menu->exec(list1->pos()); //list1->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); //list1->setMaximumHeight(60); } if (event->type()==QEvent::ToolTip) { } } if(object==customerList) { if(event->type()==QEvent::Leave) { for(int i=0;i<list1->count()-1;i++) { list1->setRowHidden(i,true); } //list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //list1->setMaximumHeight(20); customerList->hide(); } } return false;
}@
-
but i dont want it to be a seperate window perhaps i can use the widget as a popup or tootip but i am not being able to implement it do you have any idea on how to do it or any other way to implement my requirement