Insert QSplitter in QStackedWidget - c++

I have a QStackedWidget with a custom widget in it.
//mainwindow.h
private:
CentralWidget m_centralWidget;
//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->stackedWidget->addWidget(&m_centralWidget);
}
Now i want to create a QSplitter, fill the Splitter with content and add this Splitter to the StackedWidget.
CentralWidget::CentralWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::CentralWidget)
{
ui->setupUi(this);
connect(ui->pbAddSplit, SIGNAL(pressed()), this, SLOT(addSplit()));
}
void CentralWidget::addSplit()
{
QWidget* parent = parentWidget();
QStackedWidget* stackedWidget = qobject_cast<QStackedWidget*>(parent);
if(!stackedWidget) {
return;
}
QSplitter* splitter = new QSplitter(Qt::Vertical);
splitter->addWidget(new QLabel("Banana"));
splitter->addWidget(this);
int nIndex = stackedWidget->addWidget(splitter);
stackedWidget->setCurrentIndex(nIndex);
}
The result should be a QLabel("Banana") as one Widget of the splitter and the CentralWidget as the other splitter-widget.
But i just get the QLabel("Banana") - SplitterHandle and the CentralWidget got lost somehow...
splitter->addWidget(this) is obviously wrong...
Replacing it with splitter->addWidget(new QLabel("Cherry")) will result in a correct SplitterWidget...
The problem seems to be the parenting, but i cant figure it out.
Any suggestions would be most welcome!

Related

Create QLabel besides the constructor

I have a vector of labels, declared in the MainWindow class:
QVector<QPointer<QLabel>> labels;
And a function:
void MainWindow::testFunc()
{
for(int i = 0; i < 5; ++i) {
labels.push_back(QPointer<QLabel>(new QLabel(this)));
labels.back()->setGeometry(QRect(50 * i, 0, 50, 50));
labels.back()->setText("test");
}
}
I call this function in the class constuctor and everything is fine:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
testFunc();
}
Then I added a button and a connecting:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::testFunc);
}
And after clicking the labels aren't shown. I checked via debugger and in the both ways the labels are created and constructors for them are executed. Also I tried simple pointers, QTimer::singleShot() and QPointer<QLabel> without QVector - the result is the same.
Why the second way does't work? Why I can add a QLabel only in the constuctor?
QMainWindow alwayd should have a centralWidget that contains other chilrens. when using forms, centralWidget is automaticly created. you may manage labels in a horizontal layout and then add them to the centralWidget:
QPointer<QHBoxLayout> hBoxLayout (new QHBoxLayout);
for(int i = 0; i < 5; ++i) {
labels.push_back(QPointer<QLabel>(new QLabel));
labels.back()->setGeometry(QRect(50 * i, 0, 50, 50));
labels.back()->setText("test");
hBoxLayout->addWidget(labels.back().data());
}
ui->centralwidget->setLayout(hBoxLayout);

How can I programmatically make a window fullscreen?

How can I programmatically make a window fullscreen?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QNewScene(this);
ui->graphicsView->setScene(scene);
// here is some code that will make the window fullscreen
}
For example:
QTimer::singleShot(0, this, SLOT(showFullScreen()));
method to make your window full screen:
QWidget::showFullScreen()
check it here: https://doc.qt.io/qt-5/qwidget.html#showFullScreen

C++ QT Add a widget to scroll area without layout

so I an new to C++ and QT and I am trying to add a widget to a scroll area but without a layout as I want the widgets to be click and dragged.
Here's my code (main window.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "player_window.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
player_window *player = new player_window(ui->scrollArea_WidgetContents);
player->setGeometry(50,50,450,130);
ui->scrollArea_WidgetContents->layout()->addWidget(player);
}
Thanks!

QDialog is blank with show command

If I create my QDialog and show it modal with exec() everything works fine, but I need this no modal!
With show() the Dialog is empty!
ProgramLoading *programLoading = new ProgramLoading();
programLoading->show();
// some code
programLoading->done(0);
Constructor
ProgramLoading::ProgramLoading(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
setWindowFlags( Qt::CustomizeWindowHint ); // remove window border
}
Don't think something with Dialog code is wrong because it works with exec()!
Any hints? Thank you!
PS: I'm using QT plugin for VisualStudio 2008
mw (Default with Window Decoration):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sd = new SplashDialog();
sd->show();
QTimer::singleShot(10000,this,SLOT(closeSplashDialog()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeSplashDialog()
{
sd->close();
delete sd;
}
splash (UI having Label and Button):
SplashDialog::SplashDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SplashDialog)
{
ui->setupUi(this);
setWindowFlags( Qt::CustomizeWindowHint );
}
SplashDialog::~SplashDialog()
{
delete ui;
}
Splash Dialog opens and is being closed 10 seconds later as intended

QDialog widget with null parent always visible

I am working with qt 3.3. I need to make QDialog widget with null parent always visible not stays on top (WStyle_StaysOnTop) because this flag block access for main application. I need on screen keyboard functionality for my QDialog widget.
I hope I correctly understood the question. Here is a minimal example of what u want.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog *dia = new QDialog(this);
//Set the windowflags
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);
dia->show();
QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
//Add sample QLineEdit to test the input focus for mainwindow
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);
}
edit:
If you want to be able to minimize and maximize the dialog in question from systray you have to create the QSystrayIcon and context menu for it:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QDialog *dia = new QDialog(this);
dia->setWindowFlags(dia->windowFlags() | Qt::Tool);
dia->show();
QWidget *central = new QWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
QLineEdit *edit = new QLineEdit;
mainLayout->addWidget(edit);
central->setLayout(mainLayout);
setCentralWidget(central);
//Create the icon for systray
//NOTE this icon is application wide
QSystemTrayIcon *icon = new QSystemTrayIcon(QIcon(QPixmap("/usr/share/icons/oxygen/22x22/status/user-away.png")), dia);
icon->setVisible(true);
//Create context menu to manipulate the dialog
QMenu *contextMenu = new QMenu;
QAction *minimizeDialog = contextMenu->addAction("Minimize dialog");
QAction *restoreDialog = contextMenu->addAction("Restore dialog");
connect(minimizeDialog, SIGNAL(triggered()), dia, SLOT(hide()));
connect(restoreDialog, SIGNAL(triggered()), dia, SLOT(show()));
//Add it to the icon
icon->setContextMenu(contextMenu);
}