Results 1 to 11 of 11

Thread: pointer at QMainWindow

  1. #1
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default pointer at QMainWindow

    Hello, if i have created with QT Designer a class called ui_class.h with the main window layout and at the and i have declared this namespace:
    Qt Code:
    1. namespace Ui {
    2. class MainWindowClass: public Ui_MainWindowClass {};
    3. }
    To copy to clipboard, switch view to plain text mode 

    then i've made class.h in this way
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include "modify.h"
    3. class class : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. class();
    9. ~class();
    10. private:
    11. Ui::MainWindowClass ui;
    12. Modify *modify;
    13. };
    To copy to clipboard, switch view to plain text mode 

    and the class.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "class.h"
    3.  
    4.  
    5. class::class()
    6. {
    7. ui.setupUi(this);
    8. modify = new Modify(this);
    9.  
    10. }
    11.  
    12. server::~server(){}
    To copy to clipboard, switch view to plain text mode 

    how is the constructor in modify.h? cos i want to pass at the modify.h a pointer at the "MainWindowClass ui" to have access at all the widget that i have in ui_class.h.
    Is it possibile?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: pointer at QMainWindow

    What is Modify? If it is your class, you can implement a constructor that fits your needs and pass it the pointer you need.

  3. #3
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pointer at QMainWindow

    Quote Originally Posted by wysota View Post
    What is Modify? If it is your class, you can implement a constructor that fits your needs and pass it the pointer you need.
    Yes, it is my class. How would the constructor be?
    Qt Code:
    1. Modify::Modify(...){} //<-- ??
    To copy to clipboard, switch view to plain text mode 
    Then i'd like to have access at all the Widget elements into the ui_class.h.
    Thanks
    Last edited by mattia; 5th November 2007 at 13:14.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: pointer at QMainWindow

    Quote Originally Posted by mattia View Post
    Qt Code:
    1. class class : public QMainWindow
    To copy to clipboard, switch view to plain text mode 
    Is this legal C++?
    J-P Nurmi

  5. #5
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pointer at QMainWindow

    No, it isn't legal in C++ but i was making just an example and i didn't pay attention at the class name, next time i'll take care about it cos i know i can generate misconception

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: pointer at QMainWindow

    Make the constructor accept a pointer to QMainWindow (just like QWidgets accept a pointer to QWidgets) and store the pointer somewhere in Modify.

  7. #7
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pointer at QMainWindow

    I'made a little example to explain me in the best way, I created a simple GUI with a menu and a label, when i click on the menu voice i'd like to change the text label. To do this i created a class doSmt.h that perform this work.
    I want to pass a QMainWindow pointer to the class doSmt to have the access at all the attributes defined in ui_guiwid.h.
    In the class guiwid i cant acces to them simply with ui.nomeAttribute, i'd like to do the same thing into doSmt class. Thx

    ui_guiwid.h
    Qt Code:
    1. #ifndef UI_GUIWID_H
    2. #define UI_GUIWID_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QLabel>
    9. #include <QtGui/QMainWindow>
    10. #include <QtGui/QMenu>
    11. #include <QtGui/QMenuBar>
    12. #include <QtGui/QStatusBar>
    13. #include <QtGui/QWidget>
    14.  
    15. class Ui_MainWindow
    16. {
    17. public:
    18. QAction *actionChangeLabel;
    19. QWidget *centralwidget;
    20. QLabel *label;
    21. QMenuBar *menubar;
    22. QMenu *menuFIle;
    23. QStatusBar *statusbar;
    24.  
    25. void setupUi(QMainWindow *MainWindow)
    26. {
    27. if (MainWindow->objectName().isEmpty())
    28. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    29. MainWindow->resize(420, 510);
    30. actionChangeLabel = new QAction(MainWindow);
    31. actionChangeLabel->setObjectName(QString::fromUtf8("actionChangeLabel"));
    32. centralwidget = new QWidget(MainWindow);
    33. centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    34. label = new QLabel(centralwidget);
    35. label->setObjectName(QString::fromUtf8("label"));
    36. label->setGeometry(QRect(20, 10, 321, 21));
    37. MainWindow->setCentralWidget(centralwidget);
    38. menubar = new QMenuBar(MainWindow);
    39. menubar->setObjectName(QString::fromUtf8("menubar"));
    40. menubar->setGeometry(QRect(0, 0, 420, 28));
    41. menuFIle = new QMenu(menubar);
    42. menuFIle->setObjectName(QString::fromUtf8("menuFIle"));
    43. MainWindow->setMenuBar(menubar);
    44. statusbar = new QStatusBar(MainWindow);
    45. statusbar->setObjectName(QString::fromUtf8("statusbar"));
    46. MainWindow->setStatusBar(statusbar);
    47.  
    48. menubar->addAction(menuFIle->menuAction());
    49. menuFIle->addAction(actionChangeLabel);
    50.  
    51. retranslateUi(MainWindow);
    52.  
    53. QMetaObject::connectSlotsByName(MainWindow);
    54. } // setupUi
    55.  
    56. void retranslateUi(QMainWindow *MainWindow)
    57. {
    58. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
    59. actionChangeLabel->setText(QApplication::translate("MainWindow", "Change Label", 0, QApplication::UnicodeUTF8));
    60. label->setText(QApplication::translate("MainWindow", "TextLabel", 0, QApplication::UnicodeUTF8));
    61. menuFIle->setTitle(QApplication::translate("MainWindow", "File", 0, QApplication::UnicodeUTF8));
    62. } // retranslateUi
    63.  
    64. };
    65.  
    66. namespace Ui {
    67. class MainWindow: public Ui_MainWindow {};
    68. } // namespace Ui
    69.  
    70. #endif // UI_GUIWID_H
    To copy to clipboard, switch view to plain text mode 

    guiwid.h
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include "ui_guiwid.h"
    3.  
    4. class guiwid : public QMainWindow
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. guiwid();
    10. ~guiwid();
    11. private slots:
    12. void callDoStm();
    13. private:
    14. Ui::MainWindow ui;
    15. };
    To copy to clipboard, switch view to plain text mode 

    guiwid.cpp
    Qt Code:
    1. #include "guiwid.h"
    2. #include "doSmt.h"
    3. #include <QtGui/QWidget>
    4.  
    5. guiwid::guiwid()
    6. {
    7. ui.setupUi(this);
    8. connect(ui.actionChangeLabel, SIGNAL(triggered()), this, SLOT(callDoStm()));
    9. }
    10.  
    11. guiwid::~guiwid()
    12. {
    13.  
    14. }
    15.  
    16. void guiwid::callDoStm(){
    17. doSmt *ds = new doSmt(this);
    18. ds->changeLabel();
    19. }
    To copy to clipboard, switch view to plain text mode 

    doSmt.h
    Qt Code:
    1. #include <QtGui/QWidget>
    2. #include <QMainWindow>
    3.  
    4. class doSmt
    5. {
    6. public:
    7. doSmt(QMainWindow *ui);
    8. ~doSmt();
    9. public:
    10. void changeLabel();
    11. QMainWindow *mainWindow;
    12. };
    To copy to clipboard, switch view to plain text mode 

    doSmt.cpp
    Qt Code:
    1. #include "doSmt.h"
    2. #include <QtGui/QWidget>
    3.  
    4. doSmt::doSmt(QMainWindow *ui)
    5. {
    6. this->mainWindow = ui;
    7. }
    8.  
    9. doSmt::~doSmt()
    10. {
    11.  
    12. }
    13.  
    14. void doSmt::changeLabel()
    15. {
    16. // i can't access to the MainWindow attributes with mainWindow->
    17. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: pointer at QMainWindow

    1. Do you need doSmt::changeLabel() to be a separate class? You can have the same functionality in guiwnd.

    2. Your problem is strictly C++ related. The ui component is private, thus other objects don't have access to it. You can either declare the doSmt class friend to the guiwnd class (not recommended), move the ui component to public section or use multiple inheritance approach (not recommended) or provide public methods (they are called getters and setters) in guiwnd to access components of the ui (that's what Qt does and preferrs). Of course you'll also need to cast that QMainWindow pointer to guiwnd pointer to be able to access its fields and methods (or better yet make your constructor accept a pointer to guiwnd instead of QMainWindow).

  9. #9
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pointer at QMainWindow

    Thanks for the explanation, no i don't need a separate class to change a label, i thought that when i'll work with more complex application maybe i'll need to get access at some elements defined into ui_guiwid.h and rather than pass all the elements at the classes that will modify them it would be better get the direct access through a pointer to main window.
    I've solved it passed Ui::MainWindow ui; as pointer to doStm and i'm storing it into the class as private member. Do you think it is a smart way?
    Thx

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: pointer at QMainWindow

    No, it's not a smart way - it breaks the Object Oriented Programming paradigm. First you make the ui private so that noone can access it and then you allow some other object to modify it behind your back. With Qt you should just use signals and slots, like so:

    Qt Code:
    1. class Parent : public QWidget{
    2. public:
    3. Parent(QWidget *parent=0) : QWidget(parent){
    4. ui.setupUi(this);
    5. obj = new childClass;
    6. connect(obj, SIGNAL(changeLabelText(const QString &)), ui.someLabel, SLOT(setText(const QString &)));
    7. }
    8. ~Parent(){}
    9. private:
    10. Ui::someUi ui;
    11. childClass *obj;
    12. };
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to wysota for this useful post:

    mattia (7th November 2007)

  12. #11
    Join Date
    Oct 2007
    Location
    Italy
    Posts
    172
    Thanks
    39
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: pointer at QMainWindow

    I followed your useful suggestions Thanks so much!

Similar Threads

  1. QMainWindow modal from non-qt window?
    By hickscorp in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2008, 09:10
  2. QMainWindow: problem changing centralWidget
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 13:00
  3. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 01:15
  4. Signals destroys my pointer
    By Jojo in forum Qt Programming
    Replies: 12
    Last Post: 7th March 2006, 21:05
  5. Replies: 18
    Last Post: 22nd February 2006, 20:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.