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
{
O_OBJECT
// ...
signals:
void dateAndTimeChanged
( const QDateTime & dateTime
);
private slots:
void onDayChanged( int day );
void onHourChanged( int hour );
void onMinuteChanged( int minute );
private:
};
// DateAndTime.cpp
DateAndTime
::DateAndTime( 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
{
Q_OBJECT
// constructor, etc.
private slots:
void onDateAndTimeChanged
( const QDateTime & dateTime
);
private:
}
// MainWindow.cpp
MainWindow
::MainWindow( QWidget * 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.
}
// 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.
}
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.
Bookmarks