I have a small problem that I can't figure out.
I'm trying to execute e.g my_exemplary_sound->stop() before closing window with the red "x" button. I can't override closeEvent() because the window isn't "mainwindow". I create new window like this :
QGraphicsView * view = new QGraphicsView();
view->show();
Related
I have a QtChart in a QDialog and I use a simple QWidget to show it on the screen. I need to resize this hart whenever the dialog window is resized by user.
This is how I add the chart to the dialog (in constructor):
// Setup chart view to show the chart
mChartView = new QChartView(mChart, ui->widget);
mChartView->setParent(this);
mChartView->resize(ui->widget->size());
mChartView->setRenderHint(QPainter::Antialiasing);
I have overrided the resizeEvent of the QDialog in my own dialog:
void CurveDialog::resizeEvent(QResizeEvent *event)
{
mChartView->resize(ui->widget->size());
}
This works, and the chart gets resized...but the problem is it is terribly slow! because it will resize for all the steps that user drags the window corner to resize it!
How can I do the resize only when there resize is done? I wanted to use a timer but this looks like a dirty hack! any better ideas?
Qt provides a layout system to manage the geometries of child widgets within a widget. A layout will arrange the size and the position of each child to ensure that it will take all the available space.
The layout will automatically resize the child widgets when the parent is resized:
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QDialog* dialog = new QDialog();
QVBoxLayout* layoutDialog = new QVBoxLayout(dialog);
QWidget* widget = new QWidget();
QVBoxLayout* layoutWidget = new QVBoxLayout(widget);
layoutDialog->addWidget(widget);
layoutWidget->addWidget(chartView);
dialog->exec();
I just can not understand one seemingly basic thing.
If we want to show our custom dialog, we can do smth like this:
OurDialog * dlg = new OurDialog; // (this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
dlg->activateWindow();
hide(); // hide MainWindow
Depending on giving a parent to constructor or not we can make taskbar icon visible or not.
But how to make the icon not only visible but also active?
Moreover, if we move the string
hide(); // hide MainWindow
before
dlg->show();
The taskbar icon will be active, but in this case we'll get a "blinking effect" on showing the dialog.
So is there any possibility to hide MainWindow, show Dialog and make the taskbar icon active?
Thank you!
I am using Qt on Ubuntu.
I have a menu on QPushButton. I want to show menu when cursor hovers over the QPushButton and close menu when cursor is moved away.
Showing a popup menu on "hover" event seems to violate the user experience, as users expect to see the popup when they click the button. This is called a menu button. If you really want to use hover event, you may subclass the QPushButton class and use its respective events. However if you would like to use a menu button, you can try this:
QMenu *menu = new QMenu();
QAction *testAction = new QAction("test menu item", this);
menu->addAction(testAction);
button->setMenu(menu);
Documentation on QPushButton::setMenu.
You have to implement your owen QPushButton. Let's start by checking the MouseMoveEvent to handle when the mouse hover the widget.
To check if the cursos pos is inside your widget:
void CustomPushButton::mousePressEvent(QMouseEvent *e) {
const QRect widgetRect = ui->followersWidget->geometry();
const QPoint mousePos = ui->followersWidget->mapFromGlobal(QCursor::pos()); // or e->pos()
if (widgetRect.contains(mousePos)) {
// Mouse inside the widget, lets show the menu
} else {
// Mouse outside the widget, if the menu is open, close it.
}
QWidget::mousePressEvent(e);
}
To show/hide the menu you could use the QMenu::popup(..), from Qt Doc:
Displays the menu so that the action atAction will be at the specified global position p. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().
I am trying to extract the cordinates of a toolbutton. So when there is a popup it always starts from the top left corner of the widget. I am doing something like this
menu_something->popup(mapToGlobal(ui.toolButton->pos()));
However the menu shows up no where close to the toolButton. Any suggestions ?
Running with QDialog (pushable and styleable ComboBox Dialog):
Get coordinates by calling this in a subclass of QToolButton
QPoint mypoint = QWidget::mapToGlobal(QPoint(0,0));
and use
QRect myrect;
myrect.setCoords(topleft.x(),topleft.y(),topleft.x()+width,10);
YourPopupDialog.setGeomentry(myrect);
YourPopupDialog.setFocus();
YourPopupDialog.show();
to position the dialog upon the button.
Edit for use with QMenu running in slot conntected to clicked():
QMenu menu;
menu.addAction("Text 0");
menu.addAction("Text 1");
menu.exec(ui->toolButtonMenuButton->mapToGlobal(QPoint(0,0)));
I have the following sequence of actions:
//MyDialog inherits QDialog and I constuct this dialog with parent = 0
MyDialog* myDialog = new MyDialog();
myDialog->exec();
myDialog->Return();
myDialog->exec();
During first call of exec the dialog is shown at the center of the screen. Then function Return shows QMessageBox::warning (with parent = 0). Then second exec call shows the dialog at the left upper corner of the screen (I want it be at the center of the screen again). If Return function doesn't show QMessageBox::warning, the second exec call shows the dialog at the center. Why does QMessageBox spoil this behaviour of QDialog::exec? And how can I fix it in my program? (I don't want to create this dialog again, I just want to show it second time)
I have noticed that with bigger dialog window size it works perfect...
Update: Actually I can replace myDialog->Return(); with QMessageBox::warning(0, "title", "message"); and I will be having the same problem. So the next code
MyDialog* myDialog = new MyDialog();
myDialog->exec();
QMessageBox::warning(0, "title", "message");
myDialog->exec();
contains the same problem. In other words, there is problem with QDialog::exec and QMessageBox, but not with myDialog->Return();
P.S. I use Debian with Gnome 3 and Qt 5.2. I created MyDialog in QtCreator using Add New... -> Qt Designer Form Class -> Dialog with buttons right