Keep widget central forever after inserting an spaceritem - c++

I want to customize a titlebar:
class TitlebarWidget : public QWidget {
Q_OBJECT
public:
TitlebarWidget(QWidget* parent = nullptr): QWidget(parent) {
setupUi();
}
virtual ~TitlebarWidget();
private:
void setupUi(){
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
// layout->addSpacerItem(new QSpacerItem(width(), height(),
QSizePolicy::Fixed, QSizePolicy::Fixed));
m_homeButton = new QPushButton(this);
m_homeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_homeButton->setMinimumWidth(50);
m_homeButton->setMaximumWidth(50);
m_homeButton->setText(tr("Home"));
layout->addWidget(m_homeButton);
QLabel* label = new QLabel(this);
label->setText("Titlebar");
layout->addWidget(label, 0, Qt::AlignCenter);
m_synButton = new QPushButton(this);
m_synButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_synButton->setMinimumWidth(100);
m_synButton->setMaximumWidth(100);
m_homeButton->setText(tr("Sync"));
}
QPushButton* m_synButton;
QPushButton* m_homeButton;
QPushButton* m_settingButton;
QRCodeWidget* m_synPhoneWidget;
};
The titlebar is as follows:
Home button is covered by the three circles at top-left corner, so I insert an spacer-item at the beginning:
layout->addSpacerItem(new QSpacerItem(width(), height(), QSizePolicy::Fixed, QSizePolicy::Fixed));
And the titlebar is like:
I found the text 'Titlebar' was obviously deviated from the center, but I want keep it central. who can help me with a workaround or a better alternative?

Use layout->addSpacing() instead and addStretch works for me.

Related

How to reduce distance between widgets and window size with Qt?

What I currently have:
What I want:
For those unable to view the images; the widgets are spread out by some sort of margin between them. I would like to keep them as close as possible. How can I squeeze the widgets closer together?
I have already tried:
setFixedSize(sizeHint()); and setSizeConstraint(QLayout::SetFixedSize); on the main window, layouts, and widget object. Nothing seems to work.
As an extra, I would also appreciate this:
(having the label get even closer to the lineEdit)
I am using Windows and Qt 5.11.1, 64-bits.
The window constructor code:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
widget = new QWidget();
label = new QLabel(tr("Enter your name:"));
nameLine = new QLineEdit;
nameLine->setMinimumWidth(250);
label->setBuddy(nameLine);
okButton = new QPushButton (tr("Ok"));
clearButton = new QPushButton (tr("Clear"));
connect(okButton, SIGNAL(clicked()), this, SLOT(message()));
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
QGridLayout *grid = new QGridLayout;
grid->addWidget(label,0,0);
grid->addWidget(nameLine,1,0);
grid->addWidget(okButton,0,1);
grid->addWidget(clearButton,1,1);
widget->setLayout(grid);
setWindowTitle(tr("Leo v0.0"));
setCentralWidget(widget);
}
A possible solution is to establish a QVBoxLayout with addStretch():
QVBoxLayout *vlay = new QVBoxLayout;
QGridLayout *grid = new QGridLayout;
grid->addWidget(label, 0, 0);
grid->addWidget(nameLine, 1, 0);
grid->addWidget(okButton, 0, 1);
grid->addWidget(clearButton, 1, 1);
vlay->addLayout(grid);
vlay->addStretch();
widget->setLayout(vlay);
setCentralWidget(widget);

QT dynamically generate label, LineEdit, Button

I want to do serial communication in the QT inside, according to the number of serial ports to dynamically generate label, LineEdit, Button, and these three buttons can pull down the scroll bar when the size of the interface, how to do well, I write this below is dead of.
The effect of encapsulation into a method
The interface was washed last
Define QGridLayout insude QScrollArea. And add your new widgets into
that layout in code. QGridLayout::addWidget
Define table where you will show several widgets in table cells. That real complicated way.
void BaseUi::BaseScrollArea()
{
QScrollArea *pArea = new QScrollArea();
QWidget *pWidget = new QWidget();
pWidget->setStyleSheet("QWidget" "{background:white;}");
m_vbox_layout = new QVBoxLayout();
m_vbox_layout->addSpacerItem(new QSpacerItem(100, 30,
QSizePolicy::Expanding, QSizePolicy::Expanding));
pWidget->setLayout(m_vbox_layout);
pArea->setWidget(pWidget);
pArea->setWidgetResizable(true);
m_main_layout = new QVBoxLayout();
m_main_layout->addWidget(pArea);
}
void BaseUi::addAutoRecordUi(QString lab_neme, QString ledit_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}
void BaseUi::addMulRecordUi(QString lab_neme, QString ledit_name, QString
but_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
QPushButton *but = new QPushButton(but_name);
but->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
layout->addWidget(but, 0, 3);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}

Why Qt::AlignTop doesn't work in QVBoxLayout that I use like main layout?

I have simple class that inherits QDialog, I add dynamically elements
and my elements are located in the center, but I want to add them at the top.
class CustomDialog : public QDialog {
Q_OBJECT
private:
QVBoxLayout *mainLayout;
CustomDialog()
{
mainLayout = new QVBoxLayout();
setLayout(mainLayout);
}
public:
void update()
{
QLabel* label = new QLabel("some text");
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->addStretch();
verLayout->setAlignment(Qt::AlignTop);
verLayout->addWidget(label, Qt::AlignTop);
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->addLayout(verLayout, Qt::AlignTop);
}
};
What am I doing wrong? and why my dynamically added elements always in center?
I understand that you want to place it and that the top is shown, so you could use QSpacerItem to push it.
class CustomDialog : public QDialog {
Q_OBJECT
QVBoxLayout *mainLayout;
public:
CustomDialog(QWidget *parent=0): QDialog(parent)
{
mainLayout = new QVBoxLayout(this);
QSpacerItem *verticalSpacer = new QSpacerItem(20, 217, QSizePolicy::Minimum, QSizePolicy::Expanding);
mainLayout->addItem(verticalSpacer);
addWidgets("1");
addWidgets("2");
}
private:
void addWidgets(const QString &text)
{
QLabel* label = new QLabel(text);
QVBoxLayout *verLayout = new QVBoxLayout;
verLayout->addStretch();
verLayout->setAlignment(Qt::AlignTop);
verLayout->addWidget(label, Qt::AlignTop);
mainLayout->setAlignment(Qt::AlignTop);
mainLayout->insertLayout(mainLayout->count()-1, verLayout);
}
};
Or if you want it to have a reverse order you must insert it in the first position with:
mainLayout->insertLayout(0, verLayout);
Note: The use of addLayout is incorrect since the second parameter is stretch.

QResizeEvent causes SIGSEGV

My purpose is to use a class called overlay.h to add a rectangular box and text on top of a Widget (MarbleWidget). Here below is my code for the GUI. I tried to remove all unnecessary parts:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
}
MainWindow::~MainWindow()
{
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
overlay->resize(event->size()); //////////////// CAUSES SIGSEGV!!!!!!!!!!!!!
event->accept();
}
void MainWindow::setupUi(QMainWindow *MainWindow)
{
MainWindow->showMaximized();
QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth());
MainWindow->setSizePolicy(sizePolicy);
MainWindow->setTabShape(QTabWidget::Rounded);
QWidget *centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
sizePolicy.setHeightForWidth(centralwidget->sizePolicy().hasHeightForWidth());
centralwidget->setSizePolicy(sizePolicy);
centralwidget->setLayoutDirection(Qt::LeftToRight);
QGridLayout *gridLayout_4 = new QGridLayout(centralwidget);
gridLayout_4->setObjectName(QString::fromUtf8("gridLayout_4"));
QSplitter *splitter_4 = new QSplitter(centralwidget);
splitter_4->setObjectName(QString::fromUtf8("splitter_4"));
splitter_4->setOrientation(Qt::Horizontal);
QTabWidget *tabWidget_2 = new QTabWidget(splitter_4);
tabWidget_2->setObjectName(QString::fromUtf8("tabWidget_2"));
sizePolicy.setHeightForWidth(tabWidget_2->sizePolicy().hasHeightForWidth());
tabWidget_2->setSizePolicy(sizePolicy);
tabWidget_2->setMaximumSize(QSize(443, 16777));
tabWidget_2->setAutoFillBackground(true);
tabWidget_2->setTabPosition(QTabWidget::West);
tabWidget_2->setTabShape(QTabWidget::Rounded);
tabWidget_2->setIconSize(QSize(30, 16));
QWidget *tab_10 = new QWidget();
tab_10->setObjectName(QString::fromUtf8("tab_10"));
QGridLayout *gridLayout = new QGridLayout(tab_10);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
QVBoxLayout *verticalLayout_4 = new QVBoxLayout();
verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4"));
QHBoxLayout *horizontalLayout_7 = new QHBoxLayout();
horizontalLayout_7->setObjectName(QString::fromUtf8("horizontalLayout_7"));
QLabel *label_3 = new QLabel(tab_10);
label_3->setObjectName(QString::fromUtf8("label_3"));
horizontalLayout_7->addWidget(label_3);
verticalLayout_4->addLayout(horizontalLayout_7);
QTreeView *treeView_4 = new QTreeView(tab_10);
treeView_4->setObjectName(QString::fromUtf8("treeView_4"));
QStandardItemModel *standardModel = new QStandardItemModel ;
QStandardItem *rootNode = standardModel->invisibleRootItem();
treeView_4->setModel(standardModel);
verticalLayout_4->addWidget(treeView_4);
gridLayout->addLayout(verticalLayout_4, 0, 0, 1, 1);
tabWidget_2->addTab(tab_10, QString());
treeView_4->raise();
Marble::MarbleWidget* MarbleWidget = new Marble::MarbleWidget(splitter_4);
splitter_4->addWidget(MarbleWidget);
splitter_4->setStretchFactor(0,0);
splitter_4->setStretchFactor(1,6);
gridLayout_4->addWidget(splitter_4, 0, 0, 1, 1);
MainWindow->setCentralWidget(centralwidget);
MarbleWidget->raise();
tabWidget_2->raise();
overlay = new Overlay(MarbleWidget);
overlay->raise();
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
overlay.h
#include <QWidget>
#include <QPainter>
class Overlay : public QWidget
{
public:
Overlay(QWidget *parent);
protected:
void paintEvent(QPaintEvent *event);
};
overlay.cpp
#include "overlay.h"
Overlay::Overlay(QWidget *parent)
: QWidget(parent)
{
setPalette(Qt::transparent);
setAttribute(Qt::WA_TransparentForMouseEvents);
}
void Overlay::paintEvent(QPaintEvent *event)
{
QFont font;
font.setStyleHint(QFont::Helvetica, QFont::PreferAntialias);
font.setPointSize(10);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QColor(10, 10, 10, 255));
painter.fillRect(QRect(50, 50, 100, 100), QColor(100, 100, 100, 120));
painter.setFont(font);
painter.drawText(20, 20, "hi..............................");
}
The problem is that overlay->resize(event->size()); causes SIGSEGV when the core runs that line.
What is wrong with the code, how can I fix it?
The problem is solved by Thiago from freenode.
He pointed that MainWindow->showMaximized(); causes resize() event to be occurred before overlay is initialized. Removing that line or moving it to after the initialization solves the problem.
Take a look at official documentation: QWidget, Qt5 at resize method. Here you can read
Warning: Calling resize() or setGeometry() inside resizeEvent() can lead to infinite recursion.
You are doing exactly the same. To avoid infinite recursion and respectively your SIGSEGV you can (descending order of difficulty, descending order of true-way):
overwrite your resizeEvent() in Overlay and comment that line in MainWindow's resizeEvent;
in MainWindow's resizeEvent emit signal like mainWindowSizeChanged(QSize) and connect it to your Overlay's slot;
do QTimer::singleShot(...) in MainWindow's resizeEvent, so it will call a given slot after a given time interval.

QWidget - resize animation

Say I have a QHBoxLayout where there are 2 QTextEdits and between them a button with an arrow to the right. When you click on the button, the right-side QTextEdit gradually closes by moving the left border until it meets the right one. Simultaneously, the right border of the left QTextEdit takes the place which the right QTextEdit released. And after pressing on the button, the state of the system is coming to the former one.
EDIT: In order to organize this I have done the following:
1) In header file:
class MyWidget : public QWidget
{
Q_OBJECT
QTextEdit *m_textEditor1;
QTextEdit *m_textEditor2;
QPushButton *m_pushButton;
QHBoxLayout *m_layout;
int m_deltaX;
public:
MyWidget(QWidget * parent = 0);
~MyWidget(){}
private slots:
void closeOrOpenTextEdit2(bool isClosing);
};
2) In the source file:
MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
{
m_pushButton = new QPushButton(this);
m_pushButton->setText(">");
m_pushButton->setCheckable(true);
connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));
m_textEditor1 = new QTextEdit(this);
m_textEditor1->setText("AAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAA AAAAAAAAAAA AAAAAAAAAAA AA");
m_textEditor2 = new QTextEdit(this);
m_layout = new QHBoxLayout;
m_layout->addWidget(m_textEditor1);
m_layout->addWidget(m_pushButton);
m_layout->addWidget(m_textEditor2);
setLayout(m_layout);
}
void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "geometry");
QPropertyAnimation *animation2 = new QPropertyAnimation(m_pushButton, "geometry");
QPropertyAnimation *animation3 = new QPropertyAnimation(m_textEditor1, "geometry");
if(isClosing) //close the second textEdit
{
m_pushButton->setText("<");
QRect te2_1 = m_textEditor2->geometry();
m_deltaX = te2_1.width()-3;
QRect te2_2(te2_1.x()+m_deltaX, te2_1.y(), 3 ,te2_1.height());
QRect pb_1 = m_pushButton->geometry();
QRect pb_2(pb_1.x()+m_deltaX, pb_1.y(), pb_1.width() ,pb_1.height());
QRect te1_1 = m_textEditor1->geometry();
QRect te1_2(te1_1.x(), te1_1.y(), te1_1.width()+m_deltaX, te1_1.height());
//animation->setDuration(10000);
animation1->setStartValue(te2_1);
animation1->setEndValue(te2_2);
animation2->setStartValue(pb_1);
animation2->setEndValue(pb_2);
animation3->setStartValue(te1_1);
animation3->setEndValue(te1_2);
}
else //open
{
m_pushButton->setText(">");
QRect te2_1 = m_textEditor2->geometry();
QRect te2_2(te2_1.x()-m_deltaX, te2_1.y(), 3+m_deltaX ,te2_1.height());
QRect pb_1 = m_pushButton->geometry();
QRect pb_2(pb_1.x()-m_deltaX, pb_1.y(), pb_1.width() ,pb_1.height());
QRect te1_1 = m_textEditor1->geometry();
QRect te1_2(te1_1.x(), te1_1.y(), te1_1.width()-m_deltaX, te1_1.height());
//animation->setDuration(10000);
animation1->setStartValue(te2_1);
animation1->setEndValue(te2_2);
animation2->setStartValue(pb_1);
animation2->setEndValue(pb_2);
animation3->setStartValue(te1_1);
animation3->setEndValue(te1_2);
}
animation1->start();
animation2->start();
animation3->start();
}
EDIT:
And I have the following problem:
When I close the second QTextEdit (by clicking on the button) and resize the MyWidget, then the QTextEdit restores its state (but it should stay closed of course). How can I solve this problem?
Please provide me with a code snippet.
Qt's Animation framework sounds like a good place to start. You could just try to follow their tutorials, adapting for you use case. I have used it already, and it seemed quite straight forward.
1) You could replace your button with a vertical layout, place the button inside this layout and finally add a vertical spacer below the button (in same the layout).
...
QVBoxLayout* m_buttonLayout = new QVBoxLayout();
m_layout = new QHBoxLayout();
m_layout->addWidget(m_textEditor1);
m_layout->addLayout(m_buttonLayout);
m_layout->addWidget(m_textEditor2);
m_buttonLayout->addWidget(m_pushButton);
m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );
2) I guess you could (and should) animate widget's maximumSize (or just maximumWidth) property and let the layout take care of calculating actual geometries. This would also simplify your calculations. E.g.
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");
QPropertyAnimation *animation2 = new QPropertyAnimation(m_textEditor, "maximumWidth");
if (isClosing)
{
int textEdit2_start = m_textEditor2->maximumWidth();
int textEdit2_end = 0;
int textEdit_start = m_textEditor->maximumWidth();
int textEdit_end = textEdit_start + textEdit2_start;
animation1->setStartValue(textEdit2_start);
...
}
Also, now you don't have to animate buttons geometry at all (assuming that you have set fixed size to it).
PS. I didn't compile codes so there might be minor errors but you should get the idea.
Here what I wanted:
Header file
class MyWidget : public QWidget
{
Q_OBJECT
QTextEdit *m_textEditor1;
QTextEdit *m_textEditor2;
QPushButton *m_pushButton;
QHBoxLayout *m_layout;
QVBoxLayout *m_buttonLayout;
int m_deltaX;
bool m_isClosed;
public:
MyWidget(QWidget * parent = 0);
~MyWidget(){}
void resizeEvent( QResizeEvent * event );
private slots:
void closeOrOpenTextEdit2(bool isClosing);
};
Source file
MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
{
m_pushButton = new QPushButton(this);
m_pushButton->setText(">");
m_pushButton->setCheckable(true);
m_pushButton->setFixedSize(16,16);
connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));
m_textEditor1 = new QTextEdit(this);
m_textEditor1->setText("AAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAA AAAAAAAAAAA AAAAAAAAAAA AA");
m_textEditor2 = new QTextEdit(this);
m_buttonLayout = new QVBoxLayout();
m_buttonLayout->addWidget(m_pushButton);
m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );
m_layout = new QHBoxLayout;
m_layout->addWidget(m_textEditor1, 10);
m_layout->addSpacing(15);
m_layout->addLayout(m_buttonLayout);
m_layout->setSpacing(0);
m_layout->addWidget(m_textEditor2, 4);
setLayout(m_layout);
resize(800,500);
}
void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
m_isClosed = isClosing;
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");
if(isClosing) //close the second textEdit
{
m_textEditor2->setMaximumWidth(m_textEditor2->width());
int textEdit2_start = m_textEditor2->maximumWidth();
m_deltaX = textEdit2_start;
int textEdit2_end = 3;
animation1->setDuration(500);
animation1->setStartValue(textEdit2_start);
animation1->setEndValue(textEdit2_end);
m_pushButton->setText("<");
}
else //open
{
int textEdit2_start = m_textEditor2->maximumWidth();
int textEdit2_end = m_deltaX;
animation1->setDuration(500);
animation1->setStartValue(textEdit2_start);
animation1->setEndValue(textEdit2_end);
m_pushButton->setText(">");
}
animation1->start();
}
void MyWidget::resizeEvent( QResizeEvent * event )
{
if(!m_isClosed)
m_textEditor2->setMaximumWidth( QWIDGETSIZE_MAX );
}