I'm trying to add buttons to a vertical layout in QT.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
mRootLayout = new QVBoxLayout(this);
setLayout(mRootLayout);
mRootLayout->addWidget(new QPushButton("Button1", this));
mRootLayout->addWidget(new QPushButton("Button2", this));
}
I have 2 problems
1. The buttons are created on top of the menu bar
2. The buttons are not one under the other one.
I'm using a QVBoxLayout.
I think code must be change to:
mRootLayout = new QVBoxLayout(ui->centralWidget);
mRootLayout->addWidget(new QPushButton("Button1", this));
mRootLayout->addWidget(new QPushButton("Button2", this));
It's not necessary do setLayout().
Related
I want to be able to change page of QStackedWidget with some kind of animation (like fade in/out or others...)
after some research I find out maybe its possible with QGraphicsOpacityEffect, then I found this codes in here
Fade In Your Widget
// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
Fade Out Your Widget
// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(a,SIGNAL(finished()),this,SLOT(hideThisWidget()));
// now implement a slot called hideThisWidget() to do
// things like hide any background dimmer, etc.
but looks like these codes have some problem when used in QWidget inside of QStackedWidget i mean widget successfully fade in and out, but after animation finish if I minimize the windows the widget will disappear completely! (Im still able to see widget in bottom right corner of my window, looks like its pos changed?!)
btw my program is frameless.
thanks for help.
here is a example from my problem
test.cpp
Test::Test(QWidget *parent)
: CustomMainWindow(parent)
{
ui.setupUi(this);
setShadow(ui.bg_app);
connect(ui.close_app_btn, &QPushButton::clicked, this, &QWidget::close);
connect(ui.minimize_app_btn, &QPushButton::clicked, this, &QWidget::showMinimized);
QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
ui.checking->setGraphicsEffect(eff); // checking is my widget inside of QStackedWidget.
QPropertyAnimation* a = new QPropertyAnimation(eff, "opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
}
CustomMainWindow.cpp
CustomMainWindow::CustomMainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
setAttribute(Qt::WA_TranslucentBackground);
}
void CustomMainWindow::setShadow(QWidget* window)
{
QGraphicsDropShadowEffect* windowShadow = new QGraphicsDropShadowEffect;
windowShadow->setBlurRadius(9.0);
windowShadow->setColor(palette().color(QPalette::Highlight));
windowShadow->setOffset(0.0);
window->setGraphicsEffect(windowShadow);
}
when I run my program with this code, at first its successfully Fade In, but if I for example minimize the window the widget move from its original position to somewhere else, look at this gif
Note: MainWindow is the name of my class.
Header file:
//...
private slots:
void animationStackedWidgets();
void whenAnimationFinish();
//....
CPP file:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->button, &QPushButton::clicked, this, &MainWindow::animationStackedWidgets);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::animationStackedWidgets()
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
ui->stackedWidget->setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(effectSw,"opacity");
anim->setDuration(350);
anim->setStartValue(0);
anim->setEndValue(1);
anim->setEasingCurve(QEasingCurve::InBack);
anim->start(QPropertyAnimation::DeleteWhenStopped);
connect(anim, SIGNAL(finished()), this, SLOT(whenAnimationFinish()));
}
void MainWindow::whenAnimationFinish()
{
ui->stackedWidget->setGraphicsEffect(0); // remove effect
}
When having a long text applied to a QToolbutton it overlaps the menu button for the right click menu like the following:
The code for the Button
TestWindow::TestWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TestWindow)
{
ui->setupUi(this);
QMenu *menu = new QMenu();
QAction *testAction = new QAction(QLatin1String("testAction"), this);
menu->addAction(testAction);
ui->toolButton->setMenu(menu);
ui->toolButton->setText(QLatin1String("This is a long Text that should not overlap"));
ui->toolButton->setPopupMode(QToolButton::InstantPopup);
}
The toolbutton is just part of a horizontal layout of the central widget.
Is there an easy way to prevent this by e.g. cropping the text?
While using the QtStackedWidget for switching windows in a big project, it doesn't seem to take in consideration the " setWindowTitle " part added in every window, and even for the size it takes only the first size precise in the QtStackedWidget declaration. This is weird.Any clarification I'm here to read.
So My question is:
-does the QtStackedWidget allow us to change the window's title each time we switch the window?
-and what about the size is it fixed or dynamic?
From the Qt docs setWindowTitle
This property only makes sense for top-level widgets, such as windows and dialogs. If no caption has been set, the title is based of the windowFilePath. If neither of these is set, then the title is an empty string.
You can connect the QStackedWidget signals to the QMainWindow
Here is a working example:
#include <QPushButton>
#include <QHBoxLayout>
#include <QLabel>
#include <QStackedWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QWidget * poCentral = new QWidget(this);
QVBoxLayout * poVLayout = new QVBoxLayout;
QHBoxLayout * poHBtnLayout = new QHBoxLayout;
QStackedWidget * poMyStackedWidget = new QStackedWidget(this);
QPushButton * poNextPage = new QPushButton("Next page", this);
this->setWindowTitle("Page: 0");
// switch stacked pages
connect(poNextPage, &QPushButton::clicked,
[=]()
{
int iPageIndex = poMyStackedWidget->currentIndex() + 1;
if (iPageIndex >= poMyStackedWidget->count())
{
poMyStackedWidget->setCurrentIndex(0);
}
else
{
poMyStackedWidget->setCurrentIndex(iPageIndex);
}
// set window title
poMyStackedWidget->setWindowTitle(QString("Page: %1").arg(poMyStackedWidget->currentIndex()));
});
// Connect the signlas so the main window will display the title.
connect(poMyStackedWidget, &QStackedWidget::windowTitleChanged,
this, &MainWindow::setWindowTitle);
// UI layout
poHBtnLayout->addWidget(poNextPage);
poVLayout->addLayout(poHBtnLayout);
poVLayout->addWidget(poMyStackedWidget);
poCentral->setLayout(poVLayout);
// Add dumy pages
poMyStackedWidget->insertWidget(0,new QLabel("First page", this));
poMyStackedWidget->insertWidget(1,new QLabel("Second page", this));
poMyStackedWidget->insertWidget(2,new QLabel("third page", this));
this->setCentralWidget(poCentral);
}
I am trying to create an expandable Qt dialog application. The main layout is a QVBoxLayout. The top part has two views and a QPushButtonbutton. Clicking button will unfold the bottom widget which is initially hidden. In the bottom widget, there is another push button, which could fold (hide) the bottom widget. When the bottom widget fold/unfold, I expect the size of the dialog size to change as well.
But for some reason, the dialog size only increases when the bottom widget is unfolded. And never shrink back to (200, 100). Is there anything I missed?
Environment: Qt Creator 3.6.1; Based on Qt5.6.0 (MSVC2013 32bit); build on Mar 14 2016; revision d502727b2c
The code I am using :
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QTreeView *tree = new QTreeView;
QTableView *table = new QTableView;
QPushButton *button_show = new QPushButton;
button_show->setText(tr("Show hidden panel"));
QHBoxLayout *layout_top = new QHBoxLayout;
layout_top->addWidget(tree);
layout_top->addWidget(table);
layout_top->addWidget(button_show);
QHBoxLayout *layout_bottom = new QHBoxLayout;
QTextEdit *editor = new QTextEdit;
QPushButton *button_hide = new QPushButton;
button_hide->setText(tr("Hide the bottom panel"));
g_pEditor = editor;
layout_bottom->addWidget(editor);
layout_bottom->addWidget(button_hide);
QWidget *panel = new QWidget;
panel->setLayout(layout_bottom);
QVBoxLayout *layout_main = new QVBoxLayout;
layout_main->addLayout(layout_top);
layout_main->addWidget(panel);
setLayout(layout_main);
panel->hide();
connect(button_show, &QPushButton::clicked
, panel
, [=]()
{
panel->setVisible(true);
button_show->setEnabled(false);
resize(200, 200);// not really working, the dialog size is able to increase without calling resize()
});
connect(button_hide, &QPushButton::clicked, panel, [=]()
{
panel->hide();
button_show->setEnabled(true);
resize(200,100);// does not shrink the dialog size*
});
resize(200,100);
}
Thanks for your help :)
Your should try setFixedSize(w, h) instead. This sets both, the minimum and the maximum size to (w, h). "This will override the default size constraints set by QLayout."
I am using OpenSceneGraph 3.0.1 and having a problem with the Qt integration using the
osgQt::GLWidget when adding it to a tab control during startup (inside the constructor of my main window.
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget* viewerWidget = new MyViewerWidget(new osgViewer::Viewer());
ui->tabWidget->addTab(viewerWidget, "My Osg View");
// tab entry was added but nothing to see than empty Osg Window
}
It works, when calling the code from a menu after displaying the main window:
void gcdrp::MainWindow::on_actionCreate_Simulation_View_triggered()
{
QWidget* viewerWidget = new MyViewerWidget(new osgViewer::Viewer());
ui->tabWidget->addTab(viewerWidget, "My Osg View");
// tab with content is visible (as expected)
}
It seems like the scene graph is screwed up. Any ideas?
Works with setMinimumSize:
QWidget* viewerWidget = new MyViewerWidget(new osgViewer::Viewer());
viewerWidget->setMinimumSize( ui->tabWidget->width(), ui->tabWidget->height());