Connect custom widget to QWidgetTab for sizing - qt

I'm trying to get every widget to scale with change in window size. I have a main window which has a QTabWidget with a QWidget holder for each tab. I then have a custom widget with a seperate .ui file that I set to fill the QWidget space of the tab. The problem is, I can't get the contents of the QWidget to expand, only the tab and QWidget of the main window. Also, I noticed if i change the ui->setupUi( ) argument for the custom widget from "this" to "parent" the problem is fixed, and the custom widget will scale correctly. The only problem with this is none of the buttons work when I do that. The application output reads out "No Slot" found errors for the buttons. What is the correct way to make this connection?
Edit: Example code
MainWindow:: ...
{
//assign customWidget to widget placeholder on tabWidget.
//holder is just a blank widget set in gridLayout on tab widget.
CustomWidget * customWidget = new CustomWidget(ui->customWidgetHolder);
setCentralWidget(ui->tabWidget);
//This gets the sizing I want with the tabs, but
//doesn't pass it past the customWidgetHolder.
}

From what I undertand, you need to use a layout, for your custom widget inside your tab
QTabWidget* tabWidget = new QTabWidget();
QWidget* tab = new QWidget();
QVBoxLayout* verticalLayout = new QVBoxLayout(tab);
YourWidget* widget = new YourWidget(tab);
verticalLayout->addWidget(widget);
tabWidget->addTab(tab, QString());
But you'll need to be more specific (code sample ?) about the SIGNAL/SLOT connection you've made if you want answer about it.

Related

How to create a Qt dialog with dynamic number of text inputs?

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.

Adding tab to QTabWidget. is empty?

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
}

qstackedwidget cannot link to qcombobox

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.

How to delete objects inside Widget in Qt

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.

How to undock tab with osgViewer from QTabWidget?

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

Resources