Why aren't my QRadioButtons appearing in my QGroupBox? - c++

Here is my code, it's super simple:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGroupBox>
#include <QRadioButton>
#include <QVBoxLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGroupBox* genderGroupBox = new QGroupBox("Gender", ui->centralWidget);
QVBoxLayout* genderGroupBoxLayout = new QVBoxLayout(genderGroupBox);
QRadioButton* maleRadioButton = new QRadioButton("Male", genderGroupBox);
QRadioButton* femaleRadioButton = new QRadioButton("Female", genderGroupBox);
genderGroupBoxLayout->addStretch();
}
but for some reason my program looks like this:
I've programmed in Qt quite a bit and I am totally baffled as to why the radio buttons aren't showing up inside the group box. I've tried explicitly adding them with
genderGroupBoxLayout->addWidget(maleRadioButton);
and manually adding the layout via
genderGroupBox->setLayout(genderGroupBoxLayout);
even though neither should be necessary because of how they were constructed. It looks like the radio buttons don't have any parent at all.
Anyone have any ideas?
I'm coding using Qt Creator 3.1.2 on a mac. Also the .ui file is empty of everything except centralWidget, which has a grid layout.

Please refer to the documentation :
When you use a layout, you do not need to pass a parent when
constructing the child widgets. The layout will automatically reparent
the widgets (using QWidget::setParent()) so that they are children of
the widget on which the layout is installed.
Your code should look like the following :
QGroupBox* genderGroupBox = new QGroupBox("Gender");
QVBoxLayout* genderGroupBoxLayout = new QVBoxLayout;
QRadioButton* maleRadioButton = new QRadioButton("Male");
QRadioButton* femaleRadioButton = new QRadioButton("Female");
genderGroupBoxLayout->addWidget(maleRadioButton);
genderGroupBoxLayout->addWidget(femaleRadioButton);
genderGroupBoxLayout->addStretch();
genderGroupBox->setLayout(genderGroupBoxLayout);
ui->centralWidget->layout()->addWidget(genderGroupBox);

Related

Why does my toolbar dock to the incorrect location?

I have a weird scenario. I create a QMainWindow that is embedded in a QGraphicsScene. I want to have multiple QMainWindows each with a toolbar inside the scene. I'm simulating an MDI Area, without using the QMdiArea class.
Here is the MainWindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
resize(1000, 750);
qApp->setStyle(QStyleFactory::create("Fusion"));
QGraphicsView* view = new QGraphicsView;
QGraphicsScene* scene = new QGraphicsScene;
view->setFixedSize(1000, 750);
view->setScene(scene);
view->scene()->setSceneRect(-150, -150, view->size().width(), view->size().height());
setCentralWidget(view);
QWidget* widget = new QWidget;
widget->resize(300, 300);
QVBoxLayout* vLay = new QVBoxLayout;
widget->setLayout(vLay);
QMainWindow* testWindow = new QMainWindow;
testWindow->resize(300, 300);
QToolBar* toolbar = new QToolBar;
toolbar->setFloatable(false);
toolbar->setStyleSheet("border: 1px solid red"); //For better seeing the issue
toolbar->addAction("Test");
testWindow->addToolBar(toolbar);
vLay->addWidget(testWindow);
scene->addWidget(widget);
}
What happens is the QToolBar will spawn in the correct location on the embedded QMainWindow, but when it's moved and docked anywhere, it will snap too far up and too far to the left. I've added outliner code to outline the toolbar so you can see the toolbar box.
Here is the MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QToolBar>
#include <QVBoxLayout>
class MainWindow : public QMainWindow{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
};
#endif // MAINWINDOW_H
Why does the toolbar do this weird snapping effect? I added the QMainWindow into a QWidget on purpose, as it's needed for something I'm doing. I realize that embedding just the QMainWindow has the desired interaction, but I need it embedded in the QWidget. I also realize that not having parents are bad for memory management, but I handle that as well.
I'm on Qt Version 5.10.1 and I'm using Redhat as my OS.

QT TabBar side pane only

I have added a QTabWidget which is checkable. I want to hide all tabs (panes only) when the TabBar is unchecked and vice versa. Is there any way to make only pane invisible and tab bar will not disappear?
I add the image reference related to what I want in output:
Initially both tab pane is minimize and when i click on the tab it will maximize:
after clicking pane is maximize and again i click it will minimize and vice versa:
Instead of using QTabWidget. You can use a QTabBar and implement the functionality you desire hiding the corresponding widget.
here is some sample code of a new widget application example within qt creator
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QTabBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_tabbar = new QTabBar(this->centralWidget());
m_tabbar->addTab("Hello");
m_tabbar->addTab("World");
m_tabbar->setShape(QTabBar::RoundedWest);
m_tabbar->setGeometry(0,0,this->height(), 200);
connect(m_tabbar, SIGNAL(tabBarClicked(int)), this, SLOT(changedTab(int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void
MainWindow::changedTab(int idx)
{
m_tabbar->setCurrentIndex(idx);
ui->stackedWidget->setCurrentIndex(idx);
}

How to add a custom widget to the main window in Qt Creator

I am new to Qt. I took an example from here http://doc.qt.io/qt-5/qtmultimediawidgets-player-example.html.
Now I want to integrate the player in the main window. I created a Qt Widgets application project, I thought, that I would just have to edit the main window code:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Player* player;
MainWindow::setCentralWidget(player);
}
But it doesn't work and I get the following error:
Starting /home/***/Documents/build-player-Desktop-Debug/player...
The program has unexpectedly finished.
/home/***/Documents/build-player-Desktop-Debug/player crashed
How can I integrate a custom widget which is written in code, without ui in a main window? Thank you in advance.
In your own MainWindow class you can add a widget to the layout of that MainWindow:
MyMainWindow::MyMainWindow(QWidget *parent) :
...
{
this->ui->setupUi(this);
QLabel *myLabel = new QLabel();
this->layout()->addWidget(myLabel);
}
Well, player can't be placed on the window if it is not initialized.
Write something like that :
Player *player = new Player();
I usually add a QWidget (or whatever widget type I'm extending) to my .ui-file in the designer and then promote it to the actual derived type. See the Qt docs for more info on promoting widgets. This means that I can set the base widget's properties and design the window as usual but still get an instance of my special class when the UI is instantiated.
MainWindow:MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
SomeStupidWidget *ssw = new SomeStupidWidget(this); /* important! don't forget about passing "this" as argument, otherwise this could cause a memory leak(Qt handles object's lifetime by means of it's "owner" mechanism)*/
layout()->addWidget(ssw);
}

Qpushbutton not being displayed

I have the following code to display a pushbutton, i am new to qt and can't figure out why its not working.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtWidgets>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QWidget* wdg = new QWidget(this);
QPushButton *button = new QPushButton(wdg);
button->setText(tr("something"));
setCentralWidget(wdg);//line 1
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
if i remove line1 then the button is being displayed but it is not clickable.
When ui->setupUi(this); is called, the user interface that is created in Qt designer stored in .ui file is initialized. It constructs the widgets and calls setCentralWidget internally. So when it is called the central widget is replaced by the one you set.
Either make the user interface by your own and ignore ui->setupUi(this); or first call ui->setupUi(this); in the constructor and do the rest of initializations by your own if you wish.

QDockWidget causes qt to crash

I Have the version of Qt that is built into ubuntu 11.10. And am trying to use a QDockWidget that cannot actually dock (basically, I just want a window that floats. I don't want to just make the view be a top level view because then I would have the OS window bar up there, which I don't want, and if I were to hide it, then the window won't be movable).
So, I basically make a new Qt Gui project, and don't change any of the files, except for the mainwindow.cpp file, which I change to:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDockWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDockWidget *dockWidget = new QDockWidget(this);
// Without window management and attached to mainwindow (central widget)
dockWidget->setFloating( true );
// resize by frame only - not positional moveable
dockWidget->setFeatures( QDockWidget::DockWidgetMovable );
// never dock in mainwindow
dockWidget->setAllowedAreas( Qt::NoDockWidgetArea );
// title
dockWidget->setWindowTitle( "Dock Widget" );
// add contents. etc etc....
dockWidget->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
The problem is that when I go to move the widget, the whole program crashes. I want to know if I am doing something very wrong, or if there is just a bug in qt.
You made the widget floating but not floatable.
dockWidget->setFeatures( QDockWidget::DockWidgetMovable |
QDockWidget::DockWidgetFloatable );
And you can also have a movable frameless window, by handling the mouse dragging yourself through mousePressEvent, mouseReleaseEvent and mouseMoveEvent.
How to hide the now useless float button
Based on QDockWidget source code, the "float button" is not shown if there is a title bar widget:
dockWidget->setTitleBarWidget(new QLabel("Dock Widget", dockWidget));
Or since it has a name (which isn't documented), you can hide it explicitly:
QAbstractButton * floatButton =
dockWidget->findChild<QAbstractButton*>("qt_dockwidget_floatbutton");
if(floatButton)
floatButton->hide();