PDA

View Full Version : How to access ui from another class



akkarachai
26th November 2019, 10:30
I have date and time like this 13294 in DateandTime.ui and I want to send DateandTime value to Mainwindow.ui which have QtextBrowser 13295 to show the DateandTime value

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

:confused:

d_stranz
26th November 2019, 22:14
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:



// DateAndTime.h

class DateAndTime : public QWidget
{
O_OBJECT

// ...

signals:
void dateAndTimeChanged( const QDateTime & dateTime );

private slots:
void onDayChanged( int day );
void onHourChanged( int hour );
void onMinuteChanged( int minute );

private:
QDateTime mDateTime;
};

// DateAndTime.cpp

DateAndTime::DateAndTime( QWidget * pParent )
: QWidget( pParent )
{
ui.setupUi( this );

connect( dateSpinBox, &QSpinBox::valueChanged; this, &DateAndTime::onDayChanged );
// do the same for hour and minute spin boxes to connect to the other two slots

}

void DateAndTime::onDayChanged( int day )
{
// Retrieve date portion of mDateTime
QDate date = mDateTime.date();

// change the day portion
date.setDay( day );

// store it back into mDateTime
mDateTime.setDate( date );

// Tell the rest of the world about it
emit dateAndTimeChanged( mDateTime );
}

// and the equivalent for hour and minute

// MainWindow.h

class MainWindow : public QMainWindow
{
Q_OBJECT

// constructor, etc.

private slots:
void onDateAndTimeChanged( const QDateTime & dateTime );

private:
QDateTime mDateTime;
}

// MainWindow.cpp

MainWindow::MainWindow( QWidget * pParent )
: QMainWindow( pParent )
{
ui.setupUi( this );

// connect to the DateAndTime widget's signal
connect( mDateTimeWidget, &DateAndTime::dateAndTimeChanged, this, &MainWindow::onDateAndTimeChanged );
}

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

// or do whatever you want with it.
}


But I hope you realize there is a QDateTimeEdit already part of Qt which is designed for editing dates and times.

akkarachai
27th November 2019, 04:52
First of all thank you for you reply. :D

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

d_stranz
27th November 2019, 22:05
so in dateTime will store (day, h, m) right?

Do you mean QDateTime? Read the documentation.

akkarachai
28th November 2019, 02:31
I mean dateTime in this part.

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

// or do whatever you want with it.
}

d_stranz
28th November 2019, 19:29
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.