Struggling with Signals and Slots
-
Hi,
I've searched other tickets, but I've yet to find one that answers my question.
So, I've got a mainwindow which, when you click a button opens a Qt Designer Form Class.
This Qt Designer Form Class contains a calendardwidget, a cancel button and an Okay button.I'm trying to allow the user to select a date and then send this date info back to Mainwindow where the details are used.
When you click the "OK" button, the instance of QDate is created and the selected date is extracted.
If no date is selected, a default date is set.I am totally stumped as to where to declare the "connect", the "emit" and the "listen" components.
Mainwindow.cpp:
include "mainwindow.h" #include "./ui_mainwindow.h" #include "littledate.h" #include <QString> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::populateStuff() { int thisDay = 1; int thisMonth = 5; int thisYear = 2021; ui->Day->setText(QString::number(thisDay)); ui->Month->setText(QString::number(thisMonth)); ui->Year->setText(QString::number(thisYear)); } void MainWindow::on_quitButton_clicked() { close(); } void MainWindow::on_pushButton_clicked() { littleDate dateSel; dateSel.setModal(true); dateSel.exec(); populateStuff(); }
littleDate.cpp:
#include "littledate.h" #include "ui_littledate.h" littleDate::littleDate(QWidget *parent) : QDialog(parent), ui(new Ui::littleDate) { ui->setupUi(this); } littleDate::~littleDate() { delete ui; } void littleDate::on_cancelButton_clicked() { close(); } void littleDate:: setDay(int inDay) { day = inDay; } void littleDate::setMonth(int inMonth) { month = inMonth; } void littleDate::setYear(int inYear) { year = inYear; } int littleDate::getDay() { return day; } int littleDate::getMonth() { return month; } int littleDate::getYear() { return year; } void littleDate::on_okButton_clicked() { QDate date = ui->calendarWidget->selectedDate(); setDay(date.day()); setMonth(date.month()); setYear(date.year()); close(); }
Also, I'm not sure if I need to create all the mutator and accessor functions to pass the details anywhere seeing as I don't actually need to perform operations on these in this class.
Should I simply pass the QDate object back to main instead?Any hints from seasoned professionals?
Thanks.
-
Your
littleDate
class is a QDialog used in a modal fashion. When it is dismissed, with either cancel or OK, the on screen window closes and the return code tell you which. The object (dateSel
) continues to exist and you can use the getter functions (already there) to retrieve the date. So, in the main window code:void MainWindow::on_pushButton_clicked() { littleDate dateSel; dateSel.setModal(true); if (dateSel.exec() == QDialog::Accepted) { ui->Day->setText(QString::number(dateSel.getDay())); ui->Month->setText(QString::number(dateSel.getMonth())); ui->Year->setText(QString::number(dateSel.getYear())); //or give populateStuff() some arguments and pass the day, month, and year by const ref } }
Not essential, but you might as well return a QDate and reduce the number of setter/getters.