I am trying to implement a camera view app in Qt, but I don't know why It doesn't fix correctly layout / buttons. First, before to update the qlabel, I have this:
But when I click the start button. the GUI changes to this:
The code to create the window is:
cameraLabel = new QLabel(tr("No camera loaded"));
cameraLabel->setAlignment(Qt::AlignCenter);
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
cameraLabel->setBackgroundRole(QPalette::Dark);
cameraLabel->setAutoFillBackground(true);
createButtons();
...
..
.
mainLayout = new QHBoxLayout;
mainLayout->addWidget(cameraLabel);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
void CameraPlayer::createButtons()
{
QSize iconSize(36, 36);
playButton = new QToolButton;
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
playButton->setIconSize(iconSize);
playButton->setToolTip(tr("Play"));
connect(playButton, SIGNAL(clicked()), this, SLOT(play()));
snapshotButton = new QToolButton;
snapshotButton->setIcon(style()->standardIcon(QStyle::SP_DialogSaveButton));
snapshotButton->setIconSize(iconSize);
snapshotButton->setToolTip(tr("Snapshot"));
connect(snapshotButton, SIGNAL(clicked()), this, SLOT(snapshot()));
stopButton = new QToolButton; cameraLabel = new QLabel(tr("No camera loaded"));
cameraLabel->setAlignment(Qt::AlignCenter);
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
cameraLabel->setBackgroundRole(QPalette::Dark);
cameraLabel->setAutoFillBackground(true);
createButtons();
SParameters& globalParams = GlobalParameters::Instance().params;
globalParams.IPAddress = "169.251.0.1";
globalParams.isColor = true;
globalParams.timer_s =10;
timerIdCamera = 0;
mainLayout = new QHBoxLayout;
mainLayout->addWidget(cameraLabel);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
stopButton->setIconSize(iconSize);
stopButton->setToolTip(tr("Stop"));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));
colorButton = new QPushButton;
colorButton->setText(tr("WB / Color"));
connect(colorButton, SIGNAL(clicked()), this, SLOT(changeColor()));
buttonsLayout = new QVBoxLayout;
buttonsLayout->addStretch();
buttonsLayout->addWidget(playButton);
buttonsLayout->addWidget(snapshotButton);
buttonsLayout->addWidget(stopButton);
buttonsLayout->addWidget(colorButton);
buttonsLayout->addStretch();
}
To udpate the qLabel,It is configured a timer / trigger which is called every x milsecs and update the qlabel object:
void CameraPlayer::timerEvent(QTimerEvent *event){
try
{
if (timerIdCamera!=0)
{
if (camera.CameraHasImage())
{
QImage *qImage = camera.CameraGrab();
if (qImage!=NULL && SIZE_IMAGE!=QString::number(qImage->byteCount()))
{
cameraLabel->setPixmap(QPixmap::fromImage(*qImage));
cameraLabel->setFixedWidth(qImage->width());
cameraLabel->show();
}
delete[] qImage->bits();
delete qImage;
}
}
}
catch (std::exception &ex)
{
QMessageBox qMBox;
qMBox.setText(QString::fromUtf8(ex.what()));
qMBox.exec();
}
}
The problem is here
cameraLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
I was ignoring layout policies from other widgets / layouts. I commented this and It started to work well.
Related
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);
}
Recently updated from qt 5.8 to qt 5.9 and I'm getting some problems.
Pressing on a button that is not connected to any slot gives me:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Which is quite odd because before the update, it worked flawlessly. I'll paste some parts of the code so it will be a bit more clear.
First of all GUI is a class that inherits from QWidget.
#ifndef GUI_H
#define GUI_H
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include "Controller.h"
class GUI : public QWidget
{
Q_OBJECT
public:
GUI(Controller* ctrl);
void start();
~GUI();
private:
QLineEdit* txt_title;
QLineEdit* txt_desc;
QLineEdit* txt_type;
QLineEdit* txt_dur;
QPushButton* add_btn;
QPushButton* upd_btn;
QPushButton* rm_btn;
QPushButton* fil_desc_btn;
QPushButton* fil_type_btn;
QPushButton* fil_search_btn;
QPushButton* sort_title_btn;
QPushButton* sort_desc_btn;
QPushButton* sort_type_btn;
QPushButton* undo_btn;
QPushButton* refresh_btn;
QPushButton* shuffle_btn;
QListWidget* activities;
Controller* ctrl;
void setup_gui();
void connect_gui();
void add_to_list(Activity&);
void update_list();
public slots:
void add();
void edit();
void remove();
void search();
void undo();
void refresh_list();
void sort_title();
void sort_desc();
void sort_type();
void filter_desc();
void filter_type();
void shuffle();
};
#endif
And the methods that are relevant:
GUI::GUI(Controller* _ctrl)
{
this->ctrl = _ctrl;
this->setup_gui();
this->connect_gui();
setAttribute(Qt::WA_DeleteOnClose);
}
void GUI::add_to_list(Activity& activity)
{
string str = "";
str += activity.get_title() + "; " + activity.get_description() + "; " + activity.get_type() + "; " + std::to_string(activity.get_duration());
new QListWidgetItem(QString::fromStdString(str), this->activities);
}
void GUI::setup_gui()
{
this->setContentsMargins(10, 10, 10, 10);
QHBoxLayout* main_lay = new QHBoxLayout(this);
QVBoxLayout* left_lay = new QVBoxLayout();
QVBoxLayout* right_lay = new QVBoxLayout();
QHBoxLayout* title_lay = new QHBoxLayout();
QLabel* lbl_title = new QLabel(QObject::tr("title"));
lbl_title->setAlignment(Qt::AlignCenter);
this->txt_title = new QLineEdit();
QHBoxLayout* desc_lay = new QHBoxLayout();
QLabel* lbl_desc = new QLabel(QObject::tr("description"));
lbl_desc->setAlignment(Qt::AlignCenter);
this->txt_desc = new QLineEdit();
QHBoxLayout* type_lay = new QHBoxLayout();
QLabel* lbl_type = new QLabel(QObject::tr("type"));
lbl_type->setAlignment(Qt::AlignCenter);
this->txt_type = new QLineEdit();
QHBoxLayout* dur_lay = new QHBoxLayout();
QLabel* lbl_dur = new QLabel(QObject::tr("duration"));
lbl_dur->setAlignment(Qt::AlignCenter);
this->txt_dur = new QLineEdit();
QHBoxLayout* op_lay = new QHBoxLayout();
this->add_btn = new QPushButton(QObject::tr("&Add"));
this->upd_btn = new QPushButton(QObject::tr("Updat&e"));
this->rm_btn = new QPushButton(QObject::tr("&Remove"));
QHBoxLayout* fil_lay = new QHBoxLayout();
this->fil_desc_btn = new QPushButton(QObject::tr("FilterBy&Desc"));
this->fil_type_btn = new QPushButton(QObject::tr("FilterBy&Type"));
this->fil_search_btn = new QPushButton(QObject::tr("&Search"));
QHBoxLayout* undo_lay = new QHBoxLayout();
this->undo_btn = new QPushButton(QObject::tr("U&ndo"));
this->shuffle_btn = new QPushButton(QObject::tr("S&huffle"));
this->refresh_btn = new QPushButton(QObject::tr("Re&fresh"));
this->activities = new QListWidget();
main_lay->addLayout(left_lay);
main_lay->addLayout(right_lay);
title_lay->addWidget(lbl_title);
title_lay->addStretch();
title_lay->addWidget(txt_title);
desc_lay->addWidget(lbl_desc);
desc_lay->addStretch();
desc_lay->addWidget(txt_desc);
type_lay->addWidget(lbl_type);
type_lay->addStretch();
type_lay->addWidget(txt_type);
dur_lay->addWidget(lbl_dur);
dur_lay->addStretch();
dur_lay->addWidget(txt_dur);
op_lay->addWidget(add_btn);
op_lay->addWidget(upd_btn);
op_lay->addWidget(rm_btn);
fil_lay->addWidget(fil_desc_btn);
fil_lay->addWidget(fil_type_btn);
fil_lay->addWidget(fil_search_btn);
undo_lay->addWidget(refresh_btn);
undo_lay->addWidget(shuffle_btn);
undo_lay->addWidget(undo_btn);
right_lay->addLayout(title_lay);
right_lay->addLayout(desc_lay);
right_lay->addLayout(type_lay);
right_lay->addLayout(dur_lay);
right_lay->addLayout(op_lay);
right_lay->addLayout(fil_lay);
right_lay->addLayout(undo_lay);
QHBoxLayout* sort_lay = new QHBoxLayout();
this->sort_title_btn = new QPushButton(QObject::tr("SortByTit&le"));
this->sort_desc_btn = new QPushButton(QObject::tr("SortByDes&c"));
this->sort_type_btn = new QPushButton(QObject::tr("SortByT&ype"));
left_lay->addWidget(activities);
left_lay->addLayout(sort_lay);
sort_lay->addWidget(sort_title_btn);
sort_lay->addWidget(sort_desc_btn);
sort_lay->addWidget(sort_type_btn);
this->refresh_list();
}
void GUI::connect_gui()
{
QObject::connect(add_btn, SIGNAL(clicked()), this, SLOT(add()));
QObject::connect(upd_btn, SIGNAL(clicked()), this, SLOT(edit()));
QObject::connect(rm_btn, SIGNAL(clicked()), this, SLOT(remove()));
QObject::connect(sort_title_btn, SIGNAL(clicked()), this, SLOT(search()));
QObject::connect(undo_btn, SIGNAL(clicked()), this, SLOT(undo()));
QObject::connect(refresh_btn, SIGNAL(clicked()), this, SLOT(refresh_list()));
QObject::connect(fil_search_btn, SIGNAL(clicked()), this, SLOT(search()));
// QObject::connect(sort_title_btn, SIGNAL(clicked()), this, SLOT(sort_title()));
QObject::connect(sort_type_btn, SIGNAL(clicked()), this, SLOT(sort_type()));
QObject::connect(sort_desc_btn, SIGNAL(clicked()), this, SLOT(sort_desc()));
QObject::connect(shuffle_btn, SIGNAL(clicked()), this, SLOT(shuffle()));
QObject::connect(fil_desc_btn, SIGNAL(clicked()), this, SLOT(filter_desc()));
QObject::connect(fil_type_btn, SIGNAL(clicked()), this, SLOT(filter_type()));
}
Main problem is the button "sort_title_btn" which when connected to an empty method or not at all connected gives me the error when clicked.
The creation of the project was done by:
qmake -project
Editing the .pro file to add QT += core gui widgets and to include the folder with headers.
qmake
And finally: make
Compiles nicely, all buttons work except "sort_title_btn" even when NOT connected to a SLOT and "search" if the field by which the search has to be made is empty, that's not a problem for it can be fixed easily.
So, what is there to be done for the "sort_title_btn"?
I tried to put it to another SLOT, it works. Changing the name of the function I connect it to seems to bug out or something giving me the error.
In one of my windows I made a button responsible for creating dynamic array of buttons. After they are made I can't make 'anul' button close the window
Here's the code
QWidget *centralWidget = new QWidget;
int licznik=1;
QString licz;
QString kolumny = ui->lineEdit->text();
QString wiersze = ui->lineEdit_2->text();
QPushButton *button[wiersze.toInt()][kolumny.toInt()];
QGridLayout *controlsLayout = new QGridLayout;
for(int i=0;i<wiersze.toInt();i++)
{
for(int j=0;j<kolumny.toInt();j++)
{
licz = QString::number(licznik);
licznik++;
button[i][j] = new QPushButton(licz);
button[i][j]->setCheckable(1);
controlsLayout->addWidget(button[i][j], i, j);
}
}
QPushButton *okej = new QPushButton("Zatwierdź");
QPushButton *anul = new QPushButton("Anuluj");
if(anul->isDown() == true)
this->close();
controlsLayout->addWidget(okej, wiersze.toInt(), 0);
controlsLayout->addWidget(anul, wiersze.toInt(), 1);
controlsLayout->setHorizontalSpacing(0);
controlsLayout->setVerticalSpacing(0);
centralWidget->setLayout(controlsLayout);
setCentralWidget(centralWidget);
I also tried to change this->close() with centralWidget->close()
This code is executed one time only.
if(anul->isDown() == true)
this->close();
What you want is to connect the clicked() signal of the button to a function (slot)
connect(anul, SIGNAL(clicked()), this,SLOT(close()))
Because of the mouseEvent, I would expect the red and blue widgets to switch places on every click. Instead the red switches to the blue and then it never switches back, why?
Frame *red = NULL;
Frame *blue = NULL;
bool isRed = true;
Frame::Frame(QWidget *parent) :
QFrame(parent)
{
}
Frame::~Frame(){
printf("deleted.\n");
fflush(0);
}
void QLayout_clear(QLayout* layout, bool deleteWidgets){
QLayoutItem* item;
QLayout* childLayout;
while ((item = layout->takeAt(0)) != NULL){
QWidget* widget = item->widget();
if (widget != NULL){
layout->removeWidget(widget);
if (deleteWidgets){
delete widget;
}
} else if (childLayout = item->layout()){
QLayout_clear(childLayout, deleteWidgets);
}
//delete item;
}
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
this->setLayout(layout);
red = new Frame;
red->setFixedSize(100,100);
red->setStyleSheet("background-color:red");
blue = new Frame;
blue->setFixedSize(100,100);
blue->setStyleSheet("background-color:blue");
layout->addWidget(red);
}
void Widget::mouseReleaseEvent(QMouseEvent *){
printf("clicked.\n");
fflush(0);
QVBoxLayout *layout = (QVBoxLayout *)this->layout();
if (1){ //it doesnt matter if this is 1 or 0
delete layout;
layout = new QVBoxLayout;
this->setLayout(layout);
} else {
QLayout_clear(layout, false);
}
if (isRed){
layout->addWidget(blue);
isRed = false;
} else {
layout->addWidget(red);
isRed = true;
}
}
Note: here I am using a simple QFrame for the widgets to switch, in my application the widgets are much more complicated and I cannot recreate them every time I want to swap.
I believe it is not good practice from performance point of view to delete and recreate layers each time you want to switch frames, it would be easier to add both frames to the layer and then set visible only frame you want to appear. Also, boolean isRed is not necessary as info about visibility is contained within each frame itself...
Try this:
QFrame *red = NULL;
QFrame *blue = NULL;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
red = new QFrame(this);
red->setFixedSize(100,100);
red->setStyleSheet("background-color:red");
red->setHidden(false);
blue = new QFrame(this);
blue->setFixedSize(100,100);
blue->setStyleSheet("background-color:blue");
blue->setHidden(true);
layout->addWidget(red);
layout->addWidget(blue);
this->setLayout(layout);
}
void Widget::mouseReleaseEvent(QMouseEvent *){
printf("clicked.\n");
fflush(0);
red->setHidden(!red->isHidden());
blue->setHidden(!blue->isHidden());
}
The layout destructs its children. Because addWidget takes ownership so delete layout will delete red and blue widgets too.
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 );
}