switching layouts in Qt - qt

I have 2 layouts - layout1 and layout2. Let's say both are entirely different in terms of number of types of widget in them. Now I have 2 buttons but1 and but2. When but1 is cliked layout1 is activated and when but2 is clicked layout2 is activated.
How can I achieve this in Qt?
Note:
I don't want to delete my widgets( or layouts)

QStackedWidget* stackedWidget = new QStackedWidget;
QWidget* parentLayout1 = new QWidget;
QWidget* parentLayout2 = new QWidget;
QGridLayout* layout1 = new QGridLayout(parentLayout1);
QGridLayout* layout2 = new QGridLayout(parentLayout2);
stackedWidget->addWidget(parentLayout1);
stackedWidget->addWidget(parentLayout2);
stackedWidget->widget(1)->show();
Then hide and show them as you need, I tried QStackedLayout myself for the problem but I had trouble getting it working but the above solution should work fine. I used it on a project that I'm working on at the moment and the performance for switching widgets is probably the same as the QStackedLayout. Obviously what I gave you there is the bare bones of the code, but that should help you on your way. Any subsequent questions let me know. Also you can use whatever type of layout you want, doesn't have to be QGridLayout, thats just what I used.

You should use QStackedLayout or QStackedWidget for this.

Related

qt qwidgets that I create programmatically (such as Labels) never show, but ones that I create with the IDE work fine. I'm new to qt

I'm new to QT. when I create a qwidget such as a qlabel, programmatically, it never shows in my main window or anywhere else. But when I create one using the IDE it works fine. What am I missing ?
By default, widgets are set to invisible, therefore, you need to call .show() to make a widget visible. For example,
QLabel *label = new QLabel("click");
label->show();
Using the debugger I noticed that my QLabel had the wrong parent. Setting the parent correctly cured all of my problems. The other answers to my question were relevant too.

Get text() from Widget in Layout

I have created widgets from code for the first time instead of through the creator. It reads in a text file and fills a QScrollArea widget with the material name (checkbox to enable it or not) and the thickness lineEdit, as seen in the picture. The number of widgets made corresponds to the number of entries in the text file.
I create the pushbuttons and checkboxes using the following function:
void overlayers::newField(QString material, bool checked, double thickness)
{
//Create new checkbox.
QCheckBox * checkbox = new QCheckBox(material);
checkbox->setChecked(checked);
//Create new field.
QLineEdit * line = new QLineEdit(QString::number(thickness));
//Create horizontal layout.
QHBoxLayout * layout = new QHBoxLayout();
layout->addWidget(checkbox,Qt::AlignLeft);
layout->addWidget(line,Qt::AlignRight);
//Add horizontal layout to vertical.
scrollLayout->addLayout(layout);
}
Where QVBoxLayout scrollLayout is defined in the class header.
I realised after doing the whole dialog box, I have to get the values from the fields and the checkbox flags to save the data but I have no idea how to iterate through all the checkboxes and lineEdits to get the text() and checkedState() variables!
I feel I have programmed this incorrectly and that there is a better way to do it. Is it possible to do this with how I have done it? If not, how would a better programmer do it re: creating the variable number of widgets and being able to access their data? A QVector sort of thing?
Thanks heaps!
http://i.stack.imgur.com/NKJVW.png

Using layout of QMenuBar

Is it possible to get layout from QMenuBar object and add items to it. What I am trying to do is a menu bar containing custom widgets (a clock and login/off widget) on the right.
This code crashes.
QPushButton *b1 = new QPushButton("Button",ui->menuBar);
QHBoxLayout *rlayout = new QHBoxLayout(this);
ui->menuBar->layout()->addItem(rlayout);
rlayout->addWidget(b1);
b1->show();
Is there any other way?
Best regards,
Valentin Heinitz
Would it be possible to create your own container widget, put the menu bar in on the left, and the other widgets in on the right? That should get you similar functionality and appearance to the Qt3 version of the menu bar.
You should look into QWidgetAction, that allows you to insert custom widget in a menubar. You would then have to call QMenuBar::addAction(QAction*) to put your QWidgetAction in the menubar.
I know that it is not part of your question, but maybe QDockWidget would do a better job for what you need??
Hope this helps.

qt - How to add radio button to a QMainWindow

I am creating a small window to get a single input from user. For this I want to use radio buttons. In net I didn't find exact answer. There are some pallets they are adding, so just I want to know how to add QRadioButton to QMainWindow. Can any body help me?
Simply specifies the parent (your QMainWindow) when creating the RadioButton
QMainWindow *w = new QMainWindow();
QRadioButton *radiobutton = new QRadioButton(w);
w->show();
More # http://doc.qt.io/qt-4.8/qradiobutton.html

QT How to remove the action menu item

when i add the widget to the main window, by default the action menu item will be present,
how to remove that?
menuBar()->setVisible(false);
verAction = new QAction(tr("&Version"),this);
menuBar()->addAction(verAction);
connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));
displayAction = new QAction(tr("&Display"),this);
menuBar()->addAction(displayAction);
connect(displayAction, SIGNAL(triggered()),this, SLOT(displayMessage()));
exitAction = new QAction(tr("&Exit"),this);
menuBar()->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()),this, SLOT(close()));
Thanks
If you want to hide an QAction and display it when you need it, you can use the setVisible function.
If you want to remove the menu bar from the QMainWindow, you can use the QT_NO_MENUBAR preprocessor to remove all uses of a QMenuBar. If you are not using facilities provided by QMainWindow, maybe you can use a simple QWidget as main window in your application.
[Edit]
If you want to hide QActions at runtime, you will find them as member of the QMainWindow's UI. For example if you have a QAction named actionTest, you will access it like that: this->ui->actionTest->setVisible(false);
I know what you mean... you want to HIDE the DEFAULT CONTEXT MENU "Actions"....
You can do this in the Design section (not in code).
Then you see your Object-Stack on the right side like
MainWindow QMainWindow
centralWidget QWidget
webView QWebView
Now go to the property editor below...search for "contextMenuPolicy" and change it from "DefaultContextMenu" to "NoContextMenu" for every component if necessairy.
In order to remove the default context menu with the label "Actions" the following code may be used:
// Remove context menu from the all widgets.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
Essentially, the same as the Joel's answer, but the code version :)
(Code taken from QFriendFeed sample from forum.nokia.com)

Resources