data output mainwindow.cpp

model = new QSqlTableModel(this,baze); model->setTable("orders"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); ui->tableView->setModel(model); ui->tableView->hideColumn(0); ui->tableView->hideColumn(4); new_o = new newOrder(); new_o->setParent(this,Qt::Window); new_o->setModel(model); connect(new_o,SIGNAL(ready()),this,SLOT(orderAccepted())); 

neworder.cpp add data dialog

  mapper = new QDataWidgetMapper(this); mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); } void newOrder::setModel(QAbstractItemModel *model) { mapper->setModel(model); mapper->addMapping(ui->adressLineEdit,1); mapper->addMapping(ui->orderTextEdit,2,"plainText"); mapper->addMapping(ui->phoneLineEdit,3); mapper->addMapping(ui->execDateTimeEdit,5); } void newOrder::on_applyButton_clicked() { mapper->submit(); emit ready(); close(); } 

How to make it so that when you click on calendar dates (QCalendarWidget on mainWindow), the table displays data with the corresponding dates. The current date is defaulted.

    2 answers 2

    QCalendarWidget has a clicked(const QDate &date) signal clicked(const QDate &date) with the attribute of the selected date. The similar activated(const QDate &date) signal activated(const QDate &date) similar, but it works only when you double-click the mouse button or press the Enter key.

    How to change data in a table widget depends on the name and type of the field in the database table, but if we assume that it has the name date and is a string, then it all comes down to trivial filtering:

     QCalendarWidget *calendar = new QCalendarWidget(this); connect(calendar, &QCalendarWidget::clicked, [this,model](const QDate &date) { model->setFilter("`date`='%1'").arg(date.toString('yyyy-MM-dd')); }); 

    If the model already contained some data, i.e. the select() method has already been executed, its new call is not necessary (it will be executed transparently). Otherwise, when you first install the filter, you must call it:

     QCalendarWidget *calendar = new QCalendarWidget(this); connect(calendar, &QCalendarWidget::clicked, [this,model](const QDate &date) { model->setFilter("`date`='%1'").arg(date.toString('yyyy-MM-dd')); if(model->rowCount() == 0) model->select(); }); 
    • did not work ((((capture of non-variable 'MainWindow :: model' connect (calendar, & QCalendarWidget :: clicked, [this, model] (const QDate & date) well and invalid use of 'void' model->setFilter(" date` = '% 1' "). arg (date.toString ('yyyy-MM-dd')); ^ - Alexey Smirnov
    • Adapt the code to your implementation. Copy-paste will not work. - alexis031182
    • I am completely new))) I tried to do this: ui-> calendarWidget = new QCalendarWidget (); void MainWindow :: on_calendarWidget_clicked (const QDate & date) {model-> setFilter ("exec_date = date"); model-> select (); } how to make the filter? - Alexey Smirnov

    You never know who of the newbies will help. DECISION:

     ваша_модель->setFilter(QString("поле_с_датой = '%1'").arg(QDate::currentDate().toString("yyyy-MM-dd")));//текущая дата 

    so that sorting was by clicking on the dates in the calendar:

     ui->ваш_календарик = new QCalendarWidget(); void MainWindow::on_ваш_календарик_clicked(const QDate &date) { ваша_модель->setFilter(QString("поле_с_датой = '%1'").arg(date.toString("yyyy-MM-dd"))); } 

    field in database must be of type DATE ().