Is there a way to make a form/dialog with a dynamic number of text input elements (e.g. Line Edit widget)? So the user can choose to "add another" item.
If you have a dialog with a layout. You could connect a slot to a button in the dialog that will add an item by retrieving the dialog layout and adding a new item.
so something like:
void MyDialog::on_addButton_clicked()
{
QLayout *layout = layout();
if (layout) {
layout->addWidget(new QLineEdit());
}
}
And if you give the items an unique name with: setObjectName("someName") you could later use findChild<QLineEdit*>("someName"); to find all added line edits for further processing.
Well, the thing is I have a tabwidget created in qtcreator, with many tabs and in the tabs there are many lineedit and other objects.
The closeable property of the tabWidget is set to true.
I execute the program and close the tabs, but when I want to reopen the tab, it's empty, I'm using this code:
tabs->addTab(new QWidget(),"TAB 1");
I want to use the same tab create on the design of qtcreator.
Your problem is that you are adding empty widget in your code:
tabs->addTab(new QWidget(),"TAB 1");
Instead you need to keep you widgets and add them like that:
QWidget* widget; // it is stored
int index = ui->tabWidget->addTab(widget, "TAB 1");
Where to take these widgets?
It is not enough to set closable to true, you also to use signal/slot:
connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
And finally:
void YourWindow::closeTab(int index)
{
// do something else
QWidget* widget = ui->tabWidget->widget(index);
ui->tabWidget->removeTab(index);
// here you can remember it and use later when adding tab
}
I'm trying to use combo box to control multiple page. I have created some custom widget via coding and also some QFrame in widget form too. But I can't seem to find any solution to go around this. I have try to use Qstackwidget but the program hand in the process.
stackedWidget = new QStackedWidget;
parentLayout1 = new QWidget;
parentLayout2 = new QWidget;
layout1 = new QGridLayout(parentLayout1);
layout2 = new QGridLayout(parentLayout2);
//Default layout to be linearity
layout1->addWidget(ui->TimeL, 0,1);
layout1->addWidget(ui->FreqL, 0,7);
layout1->addWidget(time1, 1,1,3,4);
layout1->addWidget(ui->PlusL, 4,3);
layout1->addWidget(time2, 5,1,3,4);
layout1->addWidget(ui->EqualL, 8,3);
layout1->addWidget(time3, 9,1,3,4);
layout1->addWidget(freq1, 1,7,3,4);
layout1->addWidget(ui->PlusL2, 4,9);
layout1->addWidget(freq2, 5,7,3,4);
layout1->addWidget(ui->EqualL2, 8,9);
layout1->addWidget(freq3, 9,7,3,4);
layout1->addWidget(ui->ProFrame,0,15,3,2);
layout1->addWidget(ui->InfoFrame,10,15,2,2);
layout1->addWidget(ui->LinearFrame,3,15,7,2);
stackedWidget->widget(1)->show();
Can you help me out with this problem? Note that I have multiple Qframe in my form. But I can't seem to hide them when i only want to display 1 qframe.
You have parentLayout1 and parentLayout2 but you never added them to the stacked widget and trying to access the second widget (stackedWidget->widget(1)). Add this before that line.
stackedWidget->addWidget(parentLayout1);
stackedWidget->addWidget(parentLayout2);
Also, you haven't added anything to the second layout layout2.
I have MainWindow form which has Widget inside. And I have another Widget class (promoted to MainWindow) which has only DockWidget inside. In MainWindow I am opening new one and placing into Widget. However when I close DockWidget from close(cross"X") button. Inside my MainWindow it is not cleaning..
Sorry for my bad english better to paste my code here:
qDebug() << ui->widget->layout()->count();
QueryWidget *lQueryWidget = new QueryWidget(this);
ui->widget->layout()->addWidget(lQueryWidget);
So in everytime although I close DockWindow(lQueryWidget), layout()->count() never decrease. I tried to delete everything inside layout like ;
QList<QObject*> child = ui->widget->layout()->children();
foreach (QObject *var, child)
{
delete var;
}
But it never enters foreach loop.. If you check image below you will see that there is something above DockWidget, but it is not visible.. Could you please help me how can I solve this issue ?
To make dockable widget you should use
QDockWidget::setWidget(QWidget * widget)
The widget will be deleted when the dock widget is deleted.
You should not manipulate the dock widget layout.
I want to undock a QWidget from a QTabWiget (is set as centralWidget). The tab contains some Open Scene Graph content (OpenGL Window). When removing the Tab from the list and putting it into a new Dialog Window (=> undocking from tab) the scene data seems to be corrupt. It works with "standard widgets" but the osg seems to forget the scene.
Surprisingly, undocking works when using a QDockWidget (scene is visible after undocking the window).
Anyone knows how to undock a tab without corrupting the osgViewer?
Code called for to undock from tab and show in new dialog window:
QWidget* gv = // points to an osgViewer in a qt widget
QDialog* dlg = new QDialog(this);
dlg->setWindowTitle("hello earth");
QHBoxLayout* pMainLay = new QHBoxLayout;
gv->setMinimumSize(100,100);
gv->setGeometry(100,100,300,300);
pMainLay->addWidget(gv);
dlg->setLayout(pMainLay);
ui->tabWidget->removeTab(0); // removes the tab at position 0 (docked window)
dlg->show(); // should show the undocked dialog
There is nothing to see in the new dialog. Did I missed something?
How to "copy" the osg view properly into a new widget/dialog? Should I use a composite viewer for this kind of task? It seems there is not even the empty osg view visible (no blue canvas)...
It could be that something's going screwy when you add the osgViewer to another widget before removing it from the QTabWidget. Changing the order might help.
QWidget* gv = // points to an osgViewer in a qt widget
ui->tabWidget->removeTab(0); // removes the tab at position 0 (docked window)
QDialog* dlg = new QDialog(this);
dlg->setWindowTitle("hello earth");
QHBoxLayout* pMainLay = new QHBoxLayout;
pMainLay->addWidget(gv);
dlg->setLayout(pMainLay);
dlg->show(); // should show the undocked dialog