Solved Help on Animated side tap option sliding dialog
-
Hi everyone once again. Today, I would like to seek help to materialize the side tap option sliding dialog.
The problem occurs when I decided to move the dialog to other screen position. There is no problem with the initial setup and execution. BUT, as soon as I move the main dialog to another screen position AND execute, the sub-dialog remains on the initial setup position. (This means that it does not attached together with the main dialog, which is the problem)
Thus, I believe that I am lacking on the skill to 'bridge'/link values OR at least finding the 'moved'/updated position value into the setGeometry or position setting function. Please look at my code for clarity :) Thank you and looking forward for the community help.
//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPropertyAnimation> #include "SubDisplay_1.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void AdjustGeometryFunc(int SDposx = 300, int SDposy=300, int SDwidth=0, int SDheight=0); private: Ui::MainWindow *ui; QPropertyAnimation *animation; private slots: void openPanel(); }; #endif // MAINWINDOW_H
//Sub_Display_1.h #ifndef SubDisplay_1_H #define SubDisplay_1_H #include <QDialog> #include <QPropertyAnimation> #include <QTimer> #include "mainwindow.h" namespace Ui { class SubDisplay_1; } class SubDisplay_1 : public QDialog { Q_OBJECT public: explicit SubDisplay_1(QWidget *parent = 0); ~SubDisplay_1(); private slots: void panelMovementToOri(); private: Ui::SubDisplay_1 *ui; bool marker = 0; SubDisplay_1 *Secdialog; QTimer *tim; int initialxpos; int initialypos; int finalxpos; int finalypos; }; #endif // SubDisplay_1_H
//main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
//mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "SubDisplay_1.h" #include "SubDisplay_2.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); MainWindow::AdjustGeometryFunc(); connect(ui->pushButton_1,SIGNAL(released()),this,SLOT(openPanel())); connect(ui->pushButton_2,SIGNAL(released()),this,SLOT(openPanel())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::AdjustGeometryFunc(int SDposx, int SDposy, int SDwidth, int SDheight){ MainWindow::setGeometry(SDposx, SDposy, this->width(),this->height()); qDebug() << "SDposx" << SDposx; qDebug() << "SDposy" << SDposy; } void MainWindow::openPanel() { QPushButton * button = (QPushButton*)sender(); if (button->text() == "pushbutton_1"){ SubDisplay_1 SecDialog(this); SecDialog.setModal(false); SecDialog.exec(); int initialxpos = MainWindow::frameGeometry().x(); int initialypos = MainWindow::frameGeometry().y(); qDebug() << "initialxpos" << initialxpos; //this position value will update qDebug() << "initialypos" << initialypos; //this position value will update } }
//SubDisplay_1.cpp #include "SubDisplay_1.h" #include "ui_SubDisplay_1.h" #include "mainwindow.h" #include <QTimer> #include <QDebug> SubDisplay_1::SubDisplay_1(QWidget *parent) : QDialog(parent), ui(new Ui::SubDisplay_1) { ui->setupUi(this); MainWindow mw; initialxpos = mw.frameGeometry().x(); //PROBLEM: this is not updated. remain with initial setup value initialypos = mw.frameGeometry().y(); //PROBLEM: //PROBLEM: this is not updated. remain with initial setup value finalxpos = initialxpos - SubDisplay_1::width(); finalypos = initialypos; qDebug() << "Subinitialxpos" << initialxpos; qDebug() << "Subinitialypos" << initialypos; tim = new QTimer; tim->setInterval(1900); tim->start(); if(marker==0) { QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry"); animation->setDuration(5000); animation->setStartValue(QRect(initialxpos, initialypos, this->width(), this->height())); animation->setEndValue(QRect(finalxpos, finalypos, this->width(), this->height())); animation->start(); marker = 1; } connect(ui->pushButton_5,SIGNAL(released()),this,SLOT(panelMovementToOri())); } void SubDisplay_1::panelMovementToOri(){ if (marker==1) { QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry"); animation->setDuration(5000); animation->setStartValue(QRect(finalxpos, finalypos, this->width(), this->height())); animation->setEndValue(QRect(initialxpos, initialypos, this->width(), this->height())); animation->start(); marker = 0; connect(tim,SIGNAL(timeout()),this,SLOT(close())); } } SubDisplay_1::~SubDisplay_1() { delete ui; }
-
Hi
I think the issue comes from the fact you make a new mainwindow
ubDisplay_1::SubDisplay_1(QWidget *parent) : QDialog(parent), ui(new Ui::SubDisplay_1) { ui->setupUi(this); MainWindow mw; <<<<< thats a new copy
so when you move the real one, this copy is still in default pos.
Why dont you just use the parent parameter ?
In mainwin, you do
SubDisplay_1 SecDialog(this);so "this" is already mainwindow.
the new copy (mw) is not needed and will stay the same always.
so try
if( ! parent ) return; // if NULL u crash :) initialxpos = parent->frameGeometry().x(); //PROBLEM: this is not updated. remain with initial setup value initialypos = parent->frameGeometry().y(); //
as it will then BE the real mainwindow and have the new positions.
unlike now where is a invisible copy and always have the same default position.That is my guess :)
-
@mrjj thank you so much :( you have no idea how much relief I have to see a constructive remark/ help. I will look into it right after I'm done with my errands.
Hmm is there any good tutorial on parents? I think linkage( knowing to link) between source file and header file especially in QT is very necessary.
-
@Faruq
Hi, good to hear.The parent is part of the ownership system.
http://doc.qt.io/qt-5/objecttrees.html
Its used both or Qt to know how the widgets on the screen is related and used for
drawing. ( if parent is hidden, all child are hidden too etc)
Its also used for cleaning up. Normally in c++ when you new something , you have to call delete on it
or you leak memory. Qt helps to make that easier, and the ownership system is used to keep track of
of the children and its owners will delete them.
So when you insert say a QPushButton on Window. When window is deleted, it deletes the button too.
(so you dont have to )The key is to assign a parent/owner when you create them
QPushButton *b = new QPushButton(this) << this being mainwindow
then its shown correctly and also being delete with Mainwindow. ( that main.cpp will delete) -
@mrjj IT WORKSSSSSSSSSSSSSS!!!! OMG THANKS.
-
@mrjj I decided to continue on and encountered another problem.
I am trying to access the parent ui pushButton from SubDisplay_1 (.cpp number 2).
the code I used is;
connect(parent->pushButton_2,SIGNAL(released()), this, SLOT(panelMovementToOri()));However, parent seems to only work with adjustment and NOT to access their pushButtons or so. (Ui Objects). Did I miss out something or a step?
-
On top of the above,
how do I access a child Ui objects (E.g. pushButton, etc) and their function from its parent .cpp? -
Anyone have any idea? Currently I am working/replacing the connect with these but to no avail:
-
connect(parent->pushButton_2,SIGNAL(released()), this, SLOT(panelMovementToOri()));
-
connect(MainWindow::ui->pushButton_2,SIGNAL(released()), this, SLOT(panelMovementToOri()));
-
Ui::MainWindow *ui123 ; (UNDER HEADER PUBLIC)
connect(ui123.pushButton_2,SIGNAL(released()), this, SLOT(panelMovementToOri()));
All of the above doesn't work as it says something along Error: invalid use of non-static data member or some other error.
All I need is to connect to the parent/other ui object QPushButton from this current .ccp file. Thus the missing blank would be as follows;
aaa.cpp
There is a QPushButton here. namely PushButton_1.bbb.cpp
connect( ????????? ,SIGNAL(released()), this, SLOT(panelMovementToOri()));where the ??? is the missing blank.
-
-
Hi
You cannot connect to inner widgets. Besides being bad design, its
not possible as the UI is private.
You can use signal and slots for it.
You can easy add new public signals and the outside can use those.Here is sample that pops a dialog that then ask mainwindow to change a tab on
a widget it holds.
Same method can be used for for any kind of activation from one class to other.https://www.dropbox.com/s/w1qo7xpjhhxjzhi/myotherdialog.zip?dl=0
-
@mrjj Hi mrjj! I tried it out however there are some other parts that I am unable to pull off such as closing the newly created dialog through modeless window/dialog. The //ADDED is the modification.
https://www.dropbox.com/s/pcmjsv9fx6ydz68/Faruq.myotherdialog.zip?dl=0
Thank you and patiently waiting for your help :)
-
@mrjj I think I got it! But I would still require someone to verify if I am making sense.
I place in connect button into the MyDialog::MyDialog and it finally pull off the close button. I believe it works because this specific function have its parent declare on top while the other functions (Void MyDialog:: "the rest of it") doesnt have the parent declaration. How can I declare so that the whole file can see "Parent" as a declared variable? Must i put it in the mydialog.h Header file?
mydialog.cpp
#include "mydialog.h" #include "ui_mydialog.h" #include "mainwindow.h" #include <QDebug> MyDialog::MyDialog(QWidget* parent) : QDialog(parent), ui(new Ui::MyDialog) { ui->setupUi(this); this->setGeometry(parent->geometry().x()-(this->width()), parent->geometry().y(),this->width(),this->height()); connect(parent, SIGNAL(closetab()),this,SLOT(close())); // THE MOST IMPORTANT PART IS THE CONNECT. I believe it works because this specific function have its parent declare on top while the //other functions (Void MyDialog:: -the rest of it- doesnt have the parent declaration. }
-
Hi
There is always parent() call in any widget if you are inside other remember function.
this->parent() gives parent for any widget. ( it can be NULL)So in mainwindow, you press button to close MyDialog ?
-
@mrjj yup! my work is no longer in stale mode! I am working out and letting you know once I am done. Thank you! :) Rushing to finish and make you proud