I'm using Qt for Symbian. I have some problems with a QDialog that I open from a QMenu. The QDialog shows up fine and in the QDialog I have a QDialogButtonBox with a button to Close the QDialog. BUT if I close the QDialog and then open it from the QMenu again, it will show up but the button from the QDialogButtonBox will not show up. Instead the buttons from the QMainWindow will show but they are grayed out.
How can I get the QDialog buttons to show every time? Maybe I have some problems with setting focus on the QDialog? I really can't see what I'm doing wrong here.
It's not much code that I use, you can try it yourself. This is my code:
In QMainWindow I use the following to create the menu:
QAction *menuButton = new QAction("Menu", this);
menuButton->setSoftKeyRole(QAction::PositiveSoftKey);
QMenu *menu = new QMenu(this);
menuButton->setMenu(menu);
QAction *popup = new QAction("Show popup",this);
connect(popup, SIGNAL(triggered()), this, SLOT(showPopup()));
menu->addAction(popup);
addAction(menuButton);
This shows the QDialog:
void MyMainWindow::showPopup(){
TestDialog *test = new TestDialog(this);
test->setAttribute(Qt::WA_DeleteOnClose);
test->show();
}
This is the TestDialog:
TestDialog::TestDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
QDesktopWidget* desktopWidget = QApplication::desktop();
QRect rect = desktopWidget->availableGeometry();
this->setFixedWidth(rect.width());
}
If you want your dialog to be modal, use exec(). Otherwise, you should use show() and raise() to make sur it's on top.
Related
I'm using a QLabel as a popup to display HTML information when a cell is clicked in a QTableView. The following function is called with the row name and desired popup location when the table is clicked:
void DatabaseTableModel::showPopup (int rowIndex, const QPoint &location) const
{
QLabel *popup = new QLabel(data_[rowIndex].displayHtml(), 0, Qt::Popup);
popup->setTextFormat(Qt::RichText);
popup->setOpenExternalLinks(true);
popup->move(location);
popup->show();
}
The popup is displayed correctly at the right spot, and the HTML looks fine. On Mac with Qt 5.6, the popup closes fine when clicked outside the popup.
However, on Windows (using Qt 5.7), the popup doesn't close on a click, either inside or outside the popup. Any ideas on a fix?
I haven't seen any other answers to the question, but I found an answer on my own:
It seems that generic widget pop-ups have been obsoleted (in practice), but using it on a QDialog works OK. Here is the revised code:
void DatabaseTableModel::showPopup (int rowIndex, const QPoint &location) const {
QDialog *popup = new QDialog(0, Qt::Popup | Qt::FramelessWindowHint);
QVBoxLayout *layout = new QVBoxLayout;
QLabel *popupLabel = new QLabel(data_.value(rowIndex).displayHtml(), 0);
layout->addWidget(popupLabel);
popupLabel->setTextFormat(Qt::RichText);
popupLabel->setOpenExternalLinks(true);
popup->setLayout(layout);
popup->move(location);
popup->exec();
}
You should use QTooltip::showText. This supports HTML display and will close automatically. Tooltips are meant to display volatile information to users, QLabel is not.
I'm creating my UI from Qt Designer and it generares this code:
toolBar = new QToolBar(MainWindow);
QIcon icon;
icon.addFile(QStringLiteral(":/main"), QSize(), QIcon::Normal, QIcon::Off);
MainWindow->addToolBar(Qt::TopToolBarArea, toolBar);
actionConvert = new QAction(MainWindow);
actionConvert->setObjectName(QStringLiteral("actionConvert"));
actionConvert->setIcon(icon);
toolBar->addAction(actionConvert);
Now, back in my frame code:
QMenu *menuAdd = new QMenu (this);
menuAdd->addAction (tr("&Files..."));
menuAdd->addAction (tr("&Directory..."));
ui->actionConvert->setMenu (menuAdd);
When I run the application I can see the qaction in the toolbar even the arrow pointing down, which indicates that there is a menu, but when I click it, the menu doesn't appear...any ideas?
There does not seem to be anything wrong with your example code.
It's possible that the reason you aren't seeing the menu is that you need to press and hold the button for a few seconds in order for the menu to appear. A single click will just execute the button's normal action.
See: QToolButton::ToolButtonPopupMode.
You should add menu with menuBar() method as in my case:
void MainWindow::ueInitMenu()
{
this->ueSetCodeRegisterPlacesAction(new QAction(tr("Places"),
this));
this->ueCodeRegisterPlacesAction()->setShortcut(tr("Ctrl+P"));
this->ueCodeRegisterPlacesAction()->setStatusTip(tr("Shows places code register"));
connect(this->ueCodeRegisterPlacesAction(),
SIGNAL(triggered()),
this,
SLOT(ueSlotShowPlacesView()));
this->ueSetCodeRegisterMenu(this->menuBar()->addMenu(tr("Code register")));
this->ueCodeRegisterMenu()->addAction(this->ueCodeRegisterPlacesAction());
} // ueInitMenu
especialy the line:
this->ueSetCodeRegisterMenu(this->menuBar()->addMenu(tr("Code register")));
so in your case:
this->menuBar()->addMenu(tr("System menu");
and then add actions. Also take a look at Menus Example.
I have pretty specific situation. I want to place a QAction into QToolbar and reach following behaviour:
Checkable QAction with icon.
Classic arrow on the right side which is used for showing menu
By pressing this arrow my QDialog should appears on screen instead of QMenu-like one
Now I'm a bit confused with implementing all this things together.
For now I've created QAction added it to toolbar and also created an empty QMenubecause I didn't get the idea of how to add the "dropdown" arrow another way.
So, I also connected my slot to QMenu aboutToShow() signal and now, I can create my dialog and exec() it just before QMenu shows. But here's the main problem appears: after I did everything with my dialog an click OK button QMenu trying to appear, but as it is empty it shows nothing and further actions become available only after I left-click somwhere to "close" this menu.
Is there any way to force QMenu not to show or can inherit from QMenu and reimplemnt its behaviour (I've tried to do such trick with exec() show() popup() methods of QMenu after subclassing from it, but none of them are being called when menu appears on the screen) ?
Here's the solution, which worked for me.
class QCustomMenu : public QMenu
{
Q_OBJECT
public:
QCustomMenu(QObject *parent = 0):QMenu(parent){};
};
In code:
QAction* myActionWithMenu = new QAction ( "ActionText", toolbar);
QCustomMenu* myMenu = new QCustomMenu(toolbar);
connect(myMenu, SIGNAL(aboutToShow()), this, SLOT(execMyMenu()));
execMyMenu() implementation:
void execMyMenu(){
m_activeMenu = (QCustomMenu*)sender(); // m_activeMenu -- private member of your head class, needs to point to active custom menu
QMyDialog* dlg = new QMyDialog();
// setup your dialog with needed information
dlg->exec();
// handle return information
m_myTimer = startTimer(10); // m_myTimer -- private member of your head(MainWindow or smth like that) class
}
Now we have to handle timerEvent and close our menu:
void MyHeadClass::timerEvent(QTimerEvent *event)
{
// Check if it is our "empty"-menu timer
if ( event->timerId()==m_myTimer )
{
m_activeMenu->close(); // closing empty menu
killTimer(m_myTimer); // deactivating timer
m_myTimer = 0; // seting timer identifier to zero
m_activeMenu = NULL; // as well as active menu pointer to NULL
}
}
It works great on every platform and does what I wanted.
Hope, this would help someone. I've spent week trying to find this solution.
I'm trying to generate a dialog that contains an ad-on tool that is separate from my main program, it its triggered from an action within the menus.
I've got the following code:
void MainWindow::on_actionCalibration_Tool_triggered()
{
QGridLayout *grid = new QGridLayout;
NewDialog.setLayout(grid);
NewDialog.setMinimumHeight(500);
NewDialog.setMinimumWidth(800);
QLabel *label = new QLabel;
QFont sansFont("MS Shell Dlg 2",22, QFont::Bold);
label->setText("Test");
label->setFont(sansFont);
QPushButton *okbutton = new QPushButton;
QPushButton *closebutton = new QPushButton;
okbutton->setText("Ok");
closebutton->setText("Close");
QTimer *timer = new QTimer;
connect(okbutton,SIGNAL(clicked()),this,SLOT(on_ScanpB_clicked()));
connect(closebutton,SIGNAL(clicked()),this,SLOT(CloseDialog()));
grid->addWidget(label);
grid->addWidget(okbutton);
grid->addWidget(closebutton);
NewDialog.exec();
NewDialog.show();
}
void MainWindow::CloseDialog()
{
NewDialog.close();
}
With NewDialog being defined in main window.h as a QDialog.
My issue is when I click the close button, the dialog will close for a split second then reopen, after I click the close button for a second time it closes for good.
Is there any better implementation or way around this?
Thanks
You should not call QDialog::show and QDialog::exec. Instead, pick one to call.
Use exec if you want to block user interaction with the dialog's parent while the dialog is open. The user will not be play with anything else in the application until they dismiss the dialog. This is called a modal.
Use show if you want to allow the user to work with the dialog and the rest of the application at the same time.
Usually you'd choose exec. It is easier to work with. In your case, you displayed the dialog twice by calling both functions.
I need to add a QAction directly into a QMenuBar (not an QAction inside a QMenu but a QAction directly in the QMenuBar) I am able to do this with the following command.
ui->menuBar->addAction("VFTP",this, SLOT(VFTPmenuTrigger()) );
My only problem is that when I add it, it is appended to the end of the menu bar that I have built in Qt designer. I would like to be able to put it somewhere in the middle. Seems the only way I could do this is if I generate the menu bar by coding it only. Is there a way to build most of my menuBar in Qt Designer and then add that Qaction in the menu bar where I want? I hope I am clear and I added the QAction programmatically because it is not possible to do so with Qt (I know it sounds countintuitive to put a QAction directly in the menu bar but this is what the customer wants)
If you look at QMenuBar code you'll that it does this:
QAction *QMenuBar::addAction(const QString &text, const QObject *receiver, const char* member)
{
QAction *ret = new QAction(text, this);
QObject::connect(ret, SIGNAL(triggered(bool)), receiver, member);
addAction(ret);
return ret;
}
which basically uses QWidget::addAction(). So it stands to reason that you can use QWidget::insertAction() yourself to do the same thing. insertAction() lets you specify before what QAction you want to add.