qt - How to add radio button to a QMainWindow - qt

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

Related

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

adding graphicsitem at runtime

i need some suggestion on how to add graphicsitem according to the menu list im getting from the server.
The server will provide me the list of menu item and sub items and each sub items will have their own list of items. i will receive it using TCP socket programming as a client.
now i have to add the items and sub items received from the server to the QGraphicsScene and view .. right now im using QGraphicsLayout to add the item Horizontal and vertical but i want to know if we have any thin like
modal->view like QTableView and QListView in QGraphicsView . if so or any other method available please help me ..
You can add widgets to a QGraphicsScene with a QGraphicsProxyWidget. Create a QTableView or QListView and add it to the scene:-
QGraphicsScene* pScene = new QGraphicsScene(x, y, width, height);
QTableView* pTableView = new QTableView;
// add the widget to the scene
QGraphicsProxyWidget* pProxyTableView = pScene->addWidget(pTableView);
In adding the widget, a QGraphicsProxyWidget pointer is returned, which can be moved around and placed in the scene at the desired location: -
pProxyTableView->setPos(newXPos, newYPos);
This is explained in more detail in the documentation.

switching layouts in 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.

QLabel embedding in QStatusBar using Qt Designer

Is there any solution to embed a QLabel in QStatusBar using Qt Designer?
I don't believe so. It's fairly simple to add one programmatically, though.
If you're just wanting to show a message, you could use: statusBar()->showMessage(tr("Message Here"));, or alternatively if you really needed a QLabel on the status bar, you could do something along the lines of:
QLabel *label = new QLabel("Message");
statusBar()->addWidget(label);
label would become a child of statusBar(), and appear in the first empty spot from the bottom left (addPermanentWidget(label) would add it to the first empty spot from the bottom right). If you place QLabel label in the classes header (or other var name), you'd be able to access the variable directly later (removing the initial QLabel type from the first line, of course).
It is not possible with Qt Designer. I resolve it by creating label a in Qt Designer and later in constructor of my MainWindows add this line:
Ui::"class name of my MainWindows"::"name of statusBar Object"->addWidget("Object Name of Label");
In my application, the class name of mainwindows is MainWindowsForm, the status bar is named statusBar and the label is named informationLabel. Then I have:
Ui::MainWindowsForm::statusBar->addWidget(informationLabel);
It's not possible even if you would manually edit UI file.

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