Solved Access the UI, from multiple windows.
-
@mrjj Listen, i show you my code, cuz i am getting a little bit crazy, i can't create it in main.cpp, becasuse then how can i show it?
In MainWindow.h i have
Settings_Window* Get_Window_Data(); Settings_Window* Settings_Widget = new Settings_Window;
MainWindow.cpp
Settings_Window* MainWindow::Get_Window_Data() { return Settings_Widget; }
Window1
MainWindow B; Settings_Window S; if(B.Get_Window_Data()->ui->Checker_01->isChecked()) { }
-
@jsulm I never gonna work for anybody from software design. I am doing it more like hobby.
-
@mrjj This method is not working
Settings_Window* MainWindow::Get_Window_Data() { return Settings_Widget; }
If i press the checkerbox, it should be setted to true
void Settings_Window::on_CheckerBox_clicked() { B->Get_Window_Data()->ui->CheckerBox->setChecked(true); }
Then when i go to window one, and press the button
void Window1::on_Button001_clicked() { MainWindow* B = new MainWindow; if(B->Get_Window_Data()->ui->Auto_Reset_Data_Timer->isChecked()) { //do something } }
I don't understand all your methods, so if anyone can, please correct this stuff, cuz i have enough.
-
In what way not working ?
it just returns the pointer to your
Settings_Widget so only way it can fail is to return null.I wonder here
void Window1::on_Button001_clicked()
{MainWindow* B = new MainWindow; << you create new one. ?
so you dont have one already ?
Or is that on purpose ?
So each time you click you make a new window ? -
@mrjj I have another button to activate and show that window.
MainWindow* B = new MainWindow;
No,i have already one window.
I just use this to get MainWindow method, and try to set checkerBox as true but from ther.If i copy the same code from that button, and paste it to Window1, it's working, but is not the way how it should work, i dont want to set it ther....
I mean:
MainWindow* B = new MainWindow; B->Get_Window_Data()->ui->CheckerBox->setChecked(true); if(B->Get_Window_Data()->ui->Auto_Reset_Data_Timer->isChecked()) { //do something }
When i say "Is not working", i mean when i click the button, checkBox should return true, and when i compare it in another window, checkBox returns false, i think because i set it to another copy... How,i don't know. It should return Settings_Widget in window1.
-
Hi
Im sorry if we frustrated you. We just know from experience
what works bad over time so that is why we seem so keen to do it the right way.
Lets start over.
ok, so you have a normal main.cppint main(int argc, char* argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
so w is mainwindow
and it has the true Settings_Window embedded.So we need Window1 to have access to MainWins Settings_Window and not a new copy.
So Window1 should NOT new one it self but get the one from Mainwin.I assume Mainwindow also creates and show Window1
You can change its constructor to take a Settings_Window * and it can then store it for accessing.Like
#include "Settings_Window.h" // so it knows the TYPE ( make to use right name if not that) class Window1 : public QMainWindow { Q_OBJECT Settings_Window *GivenToMe; public: explicit Window1 (QWidget *parent = 0, Settings_Window *TheTrueOne) { GivenToMe = TheTrueOne; // store the pointer for later } ~Window1 (); .... };
// then in MainWin where you show the Window1 void MainWindow::ShowSettings() { Window1 * Win1= new Window1(this, & S_Window ); // the & to make it pointer Win1->show(); }
now inside
void Window1::SomeButtonClick() {
// you now have GivenToMe that is the Settings_Widget from MainWindow
now you can call public functions in it
if ( GivenToMe -> SomeFunc() ) ...}
If you do not want to create public access functions but go directly via UI you have to make it public
so in Settings_Window.hprivate:
Ui::Settings_Windowui;
must be
public:
Ui::Settings_Windowui;to allow
if ( GivenToMe -> ui->CheckerBox->isChecked() ) ...--
What we tried to make you do was to add public access function in mainwindow
so you would say
if ( GivenToMe ->GetSettingForX() ) ( and it just returns the ui->Checker_01->isChecked() )but if you really not want that, then you can make ui public and the syntax you want is then possible
even if its not best practice/bad idea/design.Hope this helps. :)
-
@mrjj Thank you, i like exemples rly :)
Because this is the only thing can help at least when you rly dont know what to do. Tomorrow i will try in this way.
-
I have few errors:
default argument missing for parameter 2 of 'Window1::Window1(QWidget*, Settings_Window*)'
explicit Window1(QWidget *parent = 0, Settings_Window *TheTrueOne)
^redefinition of 'Window1::Window1(QWidget*, Settings_Window*)'
Window1::Window1(QWidget *parent, Settings_Window *TheTrueOne = 0) :
^'Window1::Window1(QWidget*, Settings_Window*)' previously defined here
explicit Window1(QWidget *parent = 0, Settings_Window *TheTrueOne)
^Can you correct it? Because i tried what i could, but doesn't help.
Window1.h
#ifndef WINDOW1_H #define WINDOW1_H #include <QWidget> #include "Settings_Window.h" namespace Ui { class Window1; } class Window1 : public QWidget { Q_OBJECT Settings_Window *GivenToMe; public: explicit Window1(QWidget *parent = 0, Settings_Window *TheTrueOne) { GivenToMe = TheTrueOne; // store the pointer for later } ~Window1(); private: Ui::Window1 *ui; }; #endif // WINDOW1_H
And this is Window1.cpp
#include "window1.h" #include "ui_window1.h" #include "settings_window.h" Window1::Window1(QWidget *parent, Settings_Window *TheTrueOne = 0) : QWidget(parent), ui(new Ui::Window1) { ui->setupUi(this); } Window1::~Window1() { delete ui; }
-
Hi,
Default arguments can only be set in the function declaration. So move all
= 0
to the header.As a matter of good practice, the
parent
parameter should always be last. Question of coherency with the Qt library and what other developers expects. -
@SGaist Ok, i try that.
-
@SGaist I still have some errors the:
Error: redefinition of 'Window1::Window1(Settings_Window*, QWidget*)'
Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) : QWidget(parent),
^ -
Do you have matching declaration and implementation ?
-
Header :
explicit Window1(Settings_Window *TheTrueOne = 0, QWidget *parent = 0) { GivenToMe = TheTrueOne; }
Source:
Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) : QWidget(parent),ui(new Ui::Window1) { ui->setupUi(this); }
error: redefinition of 'Window1::Window1(Settings_Window*, QWidget*)'
Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) : QWidget(parent),ui(new Ui::Window1)
^I i remove ther Settings_Window, then i have no type specified error.
-
You wrote the method twice: once in the header and a second time in the implementation file. That's what the compilers is complaining about. You can have only one implementation.
-
@SGaist Men, this stuff is default... I just add some extra parameters... What should i remove?
Ps. Ok, i fixed it.
-
For the love of God, this stuff is not working, i lost my patience.
-
Can someone create just any working example?? I want it to compile it, and see if it's gonna work.
-
@Loc888 Come on it's simple. Just remove the method definition from the header file:
// Header : explicit Window1(Settings_Window *TheTrueOne = 0, QWidget *parent = 0); // Source: Window1::Window1(Settings_Window *TheTrueOne,QWidget *parent) : QWidget(parent),ui(new Ui::Window1), GivenToMe(TheTrueOne) { ui->setupUi(this); }
Some reading about C++ would help.
-
@jsulm I fixed that issue.. I mean, when i change the checkBox in the other window, nothing change, and i try like 50 times and more, and same stuff, doesn't work,and i can't understand why.
My compiler is giving me around 50 warnings, when i create non-static data member, it is normal?
-
@Loc888
Hi
Nope 50 warning is not normal.
What does warnings say ?Also, if nothing happens. Are you sure u are not creating a second copy/instance ?