Results 1 to 6 of 6

Thread: How to access ui from another class

  1. #1
    Join Date
    Sep 2019
    Posts
    14
    Thanks
    7
    Qt products
    Qt5
    Platforms
    MacOS X

    Default How to access ui from another class

    I have date and time like this DHM.PNG in DateandTime.ui and I want to send DateandTime value to Mainwindow.ui which have QtextBrowser Qtext.jpg to show the DateandTime value

    so how to use ui from DateandTime.ui in Mainwindow.cpp??


  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access ui from another class

    so how to use ui from DateandTime.ui in Mainwindow.cpp??
    You do not want to do this, ever.

    The Qt way is to add slots to your DateAndTime class to handle the signals sent by your three spin controls. Your class should save these values in a set of member variables. You can then create a signal for your DateAndTime class (something like dateAndTimeChanged()) which includes the new value as an argument.

    In your MainWindow class, you create a slot (onDateAndTimeChanged()) and connect it to the dateAndTimeChanged() slot.

    Something like this:

    Qt Code:
    1. // DateAndTime.h
    2.  
    3. class DateAndTime : public QWidget
    4. {
    5. O_OBJECT
    6.  
    7. // ...
    8.  
    9. signals:
    10. void dateAndTimeChanged( const QDateTime & dateTime );
    11.  
    12. private slots:
    13. void onDayChanged( int day );
    14. void onHourChanged( int hour );
    15. void onMinuteChanged( int minute );
    16.  
    17. private:
    18. QDateTime mDateTime;
    19. };
    20.  
    21. // DateAndTime.cpp
    22.  
    23. DateAndTime::DateAndTime( QWidget * pParent )
    24. : QWidget( pParent )
    25. {
    26. ui.setupUi( this );
    27.  
    28. connect( dateSpinBox, &QSpinBox::valueChanged; this, &DateAndTime::onDayChanged );
    29. // do the same for hour and minute spin boxes to connect to the other two slots
    30.  
    31. }
    32.  
    33. void DateAndTime::onDayChanged( int day )
    34. {
    35. // Retrieve date portion of mDateTime
    36. QDate date = mDateTime.date();
    37.  
    38. // change the day portion
    39. date.setDay( day );
    40.  
    41. // store it back into mDateTime
    42. mDateTime.setDate( date );
    43.  
    44. // Tell the rest of the world about it
    45. emit dateAndTimeChanged( mDateTime );
    46. }
    47.  
    48. // and the equivalent for hour and minute
    49.  
    50. // MainWindow.h
    51.  
    52. class MainWindow : public QMainWindow
    53. {
    54. Q_OBJECT
    55.  
    56. // constructor, etc.
    57.  
    58. private slots:
    59. void onDateAndTimeChanged( const QDateTime & dateTime );
    60.  
    61. private:
    62. QDateTime mDateTime;
    63. }
    64.  
    65. // MainWindow.cpp
    66.  
    67. MainWindow::MainWindow( QWidget * pParent )
    68. : QMainWindow( pParent )
    69. {
    70. ui.setupUi( this );
    71.  
    72. // connect to the DateAndTime widget's signal
    73. connect( mDateTimeWidget, &DateAndTime::dateAndTimeChanged, this, &MainWindow::onDateAndTimeChanged );
    74. }
    75.  
    76. void MainWindow::onDateAndTimeChanged( const QDateTime & dateTime )
    77. {
    78. // save it
    79. mDateTime = dateTime;
    80.  
    81. // or do whatever you want with it.
    82. }
    To copy to clipboard, switch view to plain text mode 

    But I hope you realize there is a QDateTimeEdit already part of Qt which is designed for editing dates and times.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    akkarachai (27th November 2019)

  4. #3
    Join Date
    Sep 2019
    Posts
    14
    Thanks
    7
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to access ui from another class

    First of all thank you for you reply.

    so in dateTime will store (day, h, m) right?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access ui from another class

    so in dateTime will store (day, h, m) right?
    Do you mean QDateTime? Read the documentation.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Sep 2019
    Posts
    14
    Thanks
    7
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to access ui from another class

    I mean dateTime in this part.

    void MainWindow:nDateAndTimeChanged( const QDateTime & dateTime )
    {
    // save it
    mDateTime = dateTime;

    // or do whatever you want with it.
    }

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to access ui from another class

    You did not say why you wanted to access the DateAndTime.ui from MainWindow. So the example I gave for the MainWindow onDateAndTimeChanged() slot was just to show you how to implement the slot. When the DateAndTime widget emits the signal, it also sends the current value of QDateTime that is stored in the DateAndTime class. When that signal is handled by the MainWindow slot, you should do what you want to do with it.

    Again, read the QDateTime documentation. It tells you how to extract the QDate and QTime parts from it, and these classes have methods to extract day, month, year, hour, minute, and second components.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    akkarachai (29th November 2019)

Similar Threads

  1. Replies: 1
    Last Post: 16th May 2017, 13:48
  2. Replies: 4
    Last Post: 12th November 2015, 14:00
  3. Replies: 4
    Last Post: 2nd April 2013, 10:13
  4. Replies: 4
    Last Post: 29th May 2010, 13:56
  5. Replies: 5
    Last Post: 14th July 2006, 23:42

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.