Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QCalendar Widget focus problem
QtWS25 Last Chance

QCalendar Widget focus problem

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 861 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by TheCipo76
    #1

    Hi,
    i'm using Qt 5.15.0 on Mac Os X
    in my application clicking on a tableview's cell must appear a Calendar (QCalendarWidget)

    my problem is that the calendar popup was showed behind the dialog (seem that has no focus).

    if i move the dialog with mouse and re-click on tableview the calendar appear and work fine (to select a date and insert it into tableview cell)

    how can i resolve this visualization problem?

    Thanks in advance.

    Calendario popup;
    int result1 = popup.exec();
    if(result1 == QDialog::Accepted){
        QDate date = popup.selectedDate();
        if (date < Oggi) {
            msg.setText("ATTENZIONE: Data precedente ad Oggi!");
            msg.exec();
            return;
        }            
        data = date.toString("dd-MM-yyyy");
        data.replace("-","/");
        OrdModel->itemFromIndex(index)->setText(data); // OrdModel is a tableview
    

    this is Calendario header file

    #ifndef DATEPOPUP_H_
    #define DATEPOPUP_H_
    
    #include <QDialog>
    #include <QDate>
    
    class QCalendarWidget;
    class QDialogButtonBox;
    class QVBoxLayout;
    
    class Calendario : public QDialog{
        Q_OBJECT
    public:
        Calendario(QWidget *parent=nullptr);
        QDate selectedDate() const;
    
    private:
        QWidget *widget;
        QCalendarWidget *calendarWidget;
        QDialogButtonBox* buttonBox;
        QVBoxLayout *verticalLayout;
    
    };
    
    #endif /* DATEPOPUP_H_ */
    

    and this is calendario .cpp file

    #include <QtGui>
    #include "calendario.h"
    #include <QVBoxLayout>
    #include <QCalendarWidget>
    #include <QDialogButtonBox>
    
    Calendario::Calendario(QWidget *parent)
    :QDialog(parent, Qt::Popup)
    {
        setSizeGripEnabled(false);
        resize(520, 460);
        widget = new QWidget(this);
        widget->setObjectName(QString::fromUtf8("widget"));    
        widget->setGeometry(QRect(0, 10, 516, 430));
    
        verticalLayout = new QVBoxLayout(widget);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->setContentsMargins(0, 0, 0, 0);
    
        calendarWidget = new QCalendarWidget(widget);
        calendarWidget->setObjectName(QString::fromUtf8("calendarWidget"));
    
        verticalLayout->addWidget(calendarWidget);
    
        buttonBox = new QDialogButtonBox(widget);
        buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
        buttonBox->setOrientation(Qt::Horizontal);
        buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    
        verticalLayout->addWidget(buttonBox);
    
        QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
        QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); 
    }
    
    QDate Calendario::selectedDate() const{
        return calendarWidget->selectedDate();
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • TheCipo76T TheCipo76

      Hi,
      i'm using Qt 5.15.0 on Mac Os X
      in my application clicking on a tableview's cell must appear a Calendar (QCalendarWidget)

      my problem is that the calendar popup was showed behind the dialog (seem that has no focus).

      if i move the dialog with mouse and re-click on tableview the calendar appear and work fine (to select a date and insert it into tableview cell)

      how can i resolve this visualization problem?

      Thanks in advance.

      Calendario popup;
      int result1 = popup.exec();
      if(result1 == QDialog::Accepted){
          QDate date = popup.selectedDate();
          if (date < Oggi) {
              msg.setText("ATTENZIONE: Data precedente ad Oggi!");
              msg.exec();
              return;
          }            
          data = date.toString("dd-MM-yyyy");
          data.replace("-","/");
          OrdModel->itemFromIndex(index)->setText(data); // OrdModel is a tableview
      

      this is Calendario header file

      #ifndef DATEPOPUP_H_
      #define DATEPOPUP_H_
      
      #include <QDialog>
      #include <QDate>
      
      class QCalendarWidget;
      class QDialogButtonBox;
      class QVBoxLayout;
      
      class Calendario : public QDialog{
          Q_OBJECT
      public:
          Calendario(QWidget *parent=nullptr);
          QDate selectedDate() const;
      
      private:
          QWidget *widget;
          QCalendarWidget *calendarWidget;
          QDialogButtonBox* buttonBox;
          QVBoxLayout *verticalLayout;
      
      };
      
      #endif /* DATEPOPUP_H_ */
      

      and this is calendario .cpp file

      #include <QtGui>
      #include "calendario.h"
      #include <QVBoxLayout>
      #include <QCalendarWidget>
      #include <QDialogButtonBox>
      
      Calendario::Calendario(QWidget *parent)
      :QDialog(parent, Qt::Popup)
      {
          setSizeGripEnabled(false);
          resize(520, 460);
          widget = new QWidget(this);
          widget->setObjectName(QString::fromUtf8("widget"));    
          widget->setGeometry(QRect(0, 10, 516, 430));
      
          verticalLayout = new QVBoxLayout(widget);
          verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
          verticalLayout->setContentsMargins(0, 0, 0, 0);
      
          calendarWidget = new QCalendarWidget(widget);
          calendarWidget->setObjectName(QString::fromUtf8("calendarWidget"));
      
          verticalLayout->addWidget(calendarWidget);
      
          buttonBox = new QDialogButtonBox(widget);
          buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
          buttonBox->setOrientation(Qt::Horizontal);
          buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
      
          verticalLayout->addWidget(buttonBox);
      
          QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
          QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); 
      }
      
      QDate Calendario::selectedDate() const{
          return calendarWidget->selectedDate();
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @TheCipo76
      I don't have MacOS and do not know if this is relevant (probably not, but anyway), but did you try giving your QDialog (Calendario) a parent (perhaps the QTableView) before calling popup.exec()?

      1 Reply Last reply
      0
      • TheCipo76T Offline
        TheCipo76T Offline
        TheCipo76
        wrote on last edited by TheCipo76
        #3

        i've updated the first post and add calendario.cpp code

        i gave dialog as a parent

        Calendario popup(this);
        

        but in this case the popup calendar is moved with the dialog (worse than before..) when i move it with the mouse

        always behind the dialog

        some time ago everything works great

        i didn't change anything but now don't works like before

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Why do you have that widget member variable ? It just sits on top of your dialog and does not add anything useful to the handling, even worse, if you resize your dialog, widget will not follow.

          I would remove it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • TheCipo76T Offline
            TheCipo76T Offline
            TheCipo76
            wrote on last edited by
            #5

            @SGaist said in QCalendar Widget focus problem:

            even worse, if you resize your dialog,

            ok, i've already remove it
            it was only a test that JonB suggested
            or i have misunderstand ?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              That I don't know.

              Can you show exactly how you are using it ? The code you posted does not show that in the context of your application.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • TheCipo76T Offline
                TheCipo76T Offline
                TheCipo76
                wrote on last edited by TheCipo76
                #7

                ok, i have a dialog with a tebleview

                and when i click on a specific cell ("Data C1" for example) will appear a Calendar where i can select date

                void inserimentoordini::on_tableView_pressed(const QModelIndex &index)
                {
                    QMessageBox msg;
                    //msg.setText("Riga: " + QString::number(index.row()) + " Colonna: " + QString::number(index.column()));
                    //msg.exec();
                    //Select Case
                    QDate Oggi = QDate::currentDate();
                    QString data;
                    int x = index.column();
                    switch (x)
                    {
                    case (1):
                        //Data Conf 1
                        {
                        //Calendario popup(this);
                        Calendario popup;
                        popup.setModal(true);
                        int result1 = popup.exec();
                        if(result1 == QDialog::Accepted){
                            QDate date = popup.selectedDate();
                            if (date < Oggi) {
                                msg.setText("ATTENZIONE: Data precedente ad Oggi!");
                                msg.exec();
                                return;
                            }
                            //msg.setText(date.toString("dd-MM-yyyy"));
                            //msg.exec();
                            data = date.toString("dd-MM-yyyy");
                            data.replace("-","/");
                            OrdModel->itemFromIndex(index)->setText(data);
                        }
                        return;
                        }
                    break;
                    case (3):
                        //Data Conf 2
                        {
                        Calendario popup;
                        int result2 = popup.exec();
                        if(result2 == QDialog::Accepted){
                            QDate date = popup.selectedDate();
                            //msg.setText(date.toString("dd-MM-yyyy"));
                            //msg.exec();
                            data = date.toString("dd-MM-yyyy");
                            data.replace("-","/");
                            OrdModel->itemFromIndex(index)->setText(data);
                        }
                        return;
                        }
                    break;
                    case (5):
                        //Data Conf 3
                        {
                        Calendario popup;
                        int result5 = popup.exec();
                        if(result5 == QDialog::Accepted){
                            QDate date = popup.selectedDate();
                            //msg.setText(date.toString("dd-MM-yyyy"));
                            //msg.exec();
                            data = date.toString("dd-MM-yyyy");
                            data.replace("-","/");
                            OrdModel->itemFromIndex(index)->setText(data);
                        }
                        return;
                        }
                    break;
                    case (7):
                        //Data Prev 1
                        {
                        Calendario popup;
                        int result7 = popup.exec();
                        if(result7 == QDialog::Accepted){
                            QDate date = popup.selectedDate();
                            //msg.setText(date.toString("dd-MM-yyyy"));
                            //msg.exec();
                            data = date.toString("dd-MM-yyyy");
                            data.replace("-","/");
                            OrdModel->itemFromIndex(index)->setText(data);
                        }
                        return;
                        }
                    break;
                    case (9):
                        //Data Prev 2
                        {
                        Calendario popup;
                        int result9 = popup.exec();
                        if(result9 == QDialog::Accepted){
                            QDate date = popup.selectedDate();
                            //QMessageBox msg;
                            //msg.setText(date.toString("dd-MM-yyyy"));
                            //msg.exec();
                            data = date.toString("dd-MM-yyyy");
                            data.replace("-","/");
                            OrdModel->itemFromIndex(index)->setText(data);
                        }
                        return;
                        }
                    break;
                    case (11):
                        //Data Prev 3
                        {
                        Calendario popup;
                        int result11 = popup.exec();
                        if(result11 == QDialog::Accepted){
                            QDate date = popup.selectedDate();
                            //QMessageBox msg;
                            //msg.setText(date.toString("dd-MM-yyyy"));
                            //msg.exec();
                            data = date.toString("dd-MM-yyyy");
                            data.replace("-","/");
                            OrdModel->itemFromIndex(index)->setText(data);
                        }
                        return;
                        }
                    break;
                    default:
                    //istruzioni
                    break;
                    }
                }
                

                Schermata 2020-12-15 alle 18.19.01.png

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Why not use a QStyledItemDelegate to handle that column ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  TheCipo76T 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Why not use a QStyledItemDelegate to handle that column ?

                    TheCipo76T Offline
                    TheCipo76T Offline
                    TheCipo76
                    wrote on last edited by
                    #9

                    @SGaist OK I’ll try, but that doesn’t solve my problem

                    1 Reply Last reply
                    0
                    • TheCipo76T Offline
                      TheCipo76T Offline
                      TheCipo76
                      wrote on last edited by TheCipo76
                      #10

                      i've created a new project with a mainwindow and a pushbutton

                      on pushbutton click load the dialog with the calendar..

                      everything works: popup calendar was opened and it's on top!

                      the used code is the same..

                      i can't understand where the problem is..

                      someone can help me?

                      (Schermata 2020-12-17 alle 13.36.59.png

                      i've found the problem:

                      Mainwindow: with a pushbutton i can open a first dialog and with another pushbutton a second dialog (popup Calendar) -> everything ok

                      from the first dialog using a pushbutton i will open the second dialog (popup Calendar)

                      in this case the second dialog (popup Calendar) was opened but it wasn't on the top
                      it was behind the first dialog as you can see in the following image

                      Schermata 2020-12-17 alle 17.32.25.png

                      1 Reply Last reply
                      0
                      • TheCipo76T Offline
                        TheCipo76T Offline
                        TheCipo76
                        wrote on last edited by
                        #11

                        Eureka! i've found solution:

                        setWindowFlag(Qt::WindowStaysOnTopHint);
                        
                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved