QTimeEdit - c++
-
Hi, have been having trouble with the QTimeEdit widget recently. I wanted to create a pop-up window with a time choice and a save edit. Unfortunately after creating something I cannot edit it through any keyboard input. The only thing I can do is change the hour through up/down buttons in the widget. Unfortunately, I cant even access the minute's part of the widget. Here is the code used. I would really appreciate help, cant seem to figure it out on my own :)
QWidget* popUp = new QWidget(this, Qt::Popup); popUp->setWindowModality(Qt::WindowModal); QRect rect = QStyle::alignedRect(layoutDirection(), Qt::AlignBottom, QSize(width()/4, height()/6), geometry()); popUp->setGeometry(rect); QVBoxLayout* layout = new QVBoxLayout(popUp); QTimeEdit* timeChoice = new QTimeEdit(); QTime* maxTime = new QTime(); timeChoice->activateWindow(); QPushButton *button1 = new QPushButton("SAVE"); layout->addWidget(timeChoice); layout->addWidget(button1); popUp->show(); -
It may not matter, but then again, it might. timeChoice has no owner until you add it to the layout, and I can see that as problematic if you try to activate it beforehand.
-
Hi, have been having trouble with the QTimeEdit widget recently. I wanted to create a pop-up window with a time choice and a save edit. Unfortunately after creating something I cannot edit it through any keyboard input. The only thing I can do is change the hour through up/down buttons in the widget. Unfortunately, I cant even access the minute's part of the widget. Here is the code used. I would really appreciate help, cant seem to figure it out on my own :)
QWidget* popUp = new QWidget(this, Qt::Popup); popUp->setWindowModality(Qt::WindowModal); QRect rect = QStyle::alignedRect(layoutDirection(), Qt::AlignBottom, QSize(width()/4, height()/6), geometry()); popUp->setGeometry(rect); QVBoxLayout* layout = new QVBoxLayout(popUp); QTimeEdit* timeChoice = new QTimeEdit(); QTime* maxTime = new QTime(); timeChoice->activateWindow(); QPushButton *button1 = new QPushButton("SAVE"); layout->addWidget(timeChoice); layout->addWidget(button1); popUp->show();@jakubd Hi
I've rebuilt your example (macOS, Qt 6.9.1) as follows: fresh project, empty form with a button, single slot for a button. Here'smainwindow.cpp:#include "mainwindow.h" #include "ui_mainwindow.h" #include <QTime> #include <QVBoxLayout> #include <QTimeEdit> #include <QStyle> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::btnClicked); } MainWindow::~MainWindow() { delete ui; } void MainWindow::btnClicked() { QWidget* popUp = new QWidget(this, Qt::Popup); popUp->setWindowModality(Qt::WindowModal); QRect rect = QStyle::alignedRect(layoutDirection(), Qt::AlignBottom, QSize(width()/4, height()/6), geometry()); popUp->setGeometry(rect); QVBoxLayout* layout = new QVBoxLayout(popUp); QTimeEdit* timeChoice = new QTimeEdit(); QTime* maxTime = new QTime(); timeChoice->activateWindow(); QPushButton *button1 = new QPushButton("SAVE"); layout->addWidget(timeChoice); layout->addWidget(button1); popUp->show(); }It works as it should. You can't use keys in the beginning, when the form is shown since there is no focus on the QTimeEdit. Have you tried to press the tab key once? It'll put focus on the first available widget in the queue... Which happens to be what you want. Then you can use the keys.
The remedy: in this case simple
timeChoice->grabKeyboard();would suffice but that's rather greedy option, perhaps you should examine the tab order of the modal dialog? Set the default (first) widget?