I have a Qt application and I would like to superimpose a button on a QTreeWidget using the layouts.
In fact, when I click on an item, I want the button to set visible, and centered above the tree. The button have to stay at the foreground until I click.
Is it possible ?
You could use QStackedLayout and change it's stacking mode to QStackedLayout::StackAll, when you need to show both widgets. Methods would be something like this:
void Widget::showButton()
{
stackedLayout->setStackingMode(QStackedLayout::StackAll);
stackedLayout->setCurrentWidget(widgetWithButton);
}
void Widget::hideButton()
{
stackedLayout->setCurrentWidget(treeWidget);
stackedLayout->setStackingMode(QStackedLayout::StackOne);
}
Related
I'm getting started with Qt and decided to build a full-screen text editor. I want to have a button (button with arrow in screenshot) attached to a QDockWidget which opens and closes it so the button is always visible to the right side of the screen and stay anchored to it when dock is visible or resized.
My current app is a simple fullscreen textEdit set to centeralwidget in Mainwindow.
I haven't found a way to do this yet with layouts or existing addAnchor() functions so any help or direction is appreciated.
You can achieve what you want by using a container for your text edit and the button. A QWidget instance can be used as an "invisible"*** container for other widgets.
So in Qt Designer you add a widget as a central widget of the main-window, inside this widget you add the text edit and the button, then you set a vertical layout for this container widget.
Don't forget to restrict the docking widget to only dock to the right side, you can do that with: dock->setAllowedAreas(Qt::DockWidgetArea::RightDockWidgetArea); //assuming dock is the pointer to your QDockWidget.
In case you want the dockWidget to be able to dock to any side and the button to follow, you can do that too, but it get a little bit more complicated. Basically you need to connect a slot to dockLocationChanged of your dockWidget and based on where it's docked you need to set-up a new layout for the container widget to be vertical or horizontal and the order of the textEdit and the button based on the side the dock happened.
LE:*** you will most likely need to set the margins you want, since both the widget and it's layout can have them and the actual content might have higher spacing than you want.
I currently have a QComboBox in a QGraphicsScene and I need it to detect clicks. To see if there is a widget in the clicked position, I use:
void BlockScene::mousePressEvent(QMouseEvent *event)
{
if (itemAt(event->pos()) != m_widgetItem)
{
// ...
}
}
This works well for different widgets except for combo boxes where it only takes into account the original widget and not the drop-down list that appears after a first click.
To know if it came from the scene or not, I tested also by redefining mousePressEvent of the class QComboBox and same problem: It is called only when clicking on the initial widget.
Is there a way to get the drop-down list? To detect a click on it? Ideas?
You can define a custom widget for the view of the combo, or for its completer.
For example, in a subclass of QComboBox, if you need a completer, try this code. MyListView is a subclass of QListView. On that, you can reimplement the mousePressEvent method
completer()->setCompletionMode(QCompleter::PopupCompletion);
MyListView *comboView = new MyListView();
completer()->setPopup(comboView);
if you don't need the completer, do the setView directly on the combobox.
MyListView *comboView = new MyListView();
setView(comboView);
I have a ListWidget whose elements are all icons, and I have a GraphicsView on the other side. Here is the ListWidget event code:
void MainWindow::on_zemin_buton_clicked()
{
ui->aksesuar_ornekleri->clear();
ui->aksesuar_ornekleri->setViewMode(QListWidget::IconMode);
ui->aksesuar_ornekleri->setIconSize(QSize(50,50));
ui->aksesuar_ornekleri->setResizeMode(QListWidget::Adjust);
ui->aksesuar_ornekleri->addItem(new QListWidgetItem(QIcon("../1.jpg"),"11"));
ui->aksesuar_ornekleri->addItem(new QListWidgetItem(QIcon("../2.jpg"),"12"));
ui->aksesuar_ornekleri->addItem(new QListWidgetItem(QIcon("../3.jpg"),"13"));
}
When I click the push button, it clears the list and fills it with the right icons. (I have 4-5 more button click events like this.)
I just want to drag these icons and drop them into the GraphicsView. How can I do that? Should I set up the right design settings or write some drag-drop event code myself?
Edit: Now I realize that I want to do the exact thing with the Qt GUI toolbox. I want to add icons but don't want them to disappear from my list too.
I would like to create a GUI interface as per the attached pic
[
My main issue is the central slider widget..as you can see I would like to create a function choosing widget that the user can slide left and right then click on the desired cook function..
Unfortunately it has to be done with Qt C++ widget not QML.
There are many ways to accomplish that.
One would be arranging the small widgets next to each other manually, using setGeometry(). Overlay the complete visible area of the parent with a transparent widget. Reimplement the mouseEvents in that overlay and use the move, press click events to decide how to move the small widgets (by repeatedly calling setGeometry on them with other coordinates) or whether one has been clicked.
Should be very lightweighted and straight forward to implement and allows complete control. Would be also easy to change the sizes by calling setGeometry) with a different size to model a fluid zoom effect. E.g. to have the center widget bigger than the peripheral.
You may also have a look at QScroller which should help you with the scrolling control.
How about Qt gestures ? Haven't used it but looks like your use-case.
reference:
http://doc.qt.io/qt-4.8/gestures-overview.html
http://doc.qt.io/qt-4.8/qswipegesture.html
You could capture the gesture on your widget and do actions like:
bool ImageWidget::gestureEvent(QGestureEvent *event)
{
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
swipeTriggered(static_cast<QSwipeGesture *>(swipe));
return true;
}
void ImageWidget::swipeTriggered(QSwipeGesture *gesture)
{
if (gesture->state() == Qt::GestureFinished) {
if (gesture->horizontalDirection() == QSwipeGesture::Left) {
// highlight the right widget , you could even bring it to center
} else if (gesture->horizontalDirection() == QSwipeGesture::Right) {
// highlight the left widget , you could even bring it to center
}
}
}
I have a composite control in Qt that is a QWidget with a QHBoxLayout containing 3 sub controls (QWidget > QHBoxLayout > 3 QLabels). On one of those controls, which is just a QLabel displaying an icon, I want it to change it's icon when hovering over the entire control. I can get it to work when actually hovering over that icon
QLabel#stateIcon {
image: url(:/icons/stateNormal);
}
QLabel#stateIcon:hover {
image: url(:/icons/stateHover);
}
However, I would like it to show the hover icon when the mouse is anywhere over the whole control.
I tried something like the following:
QLabel#stateIcon::parent:hover {
image: url(:/icons/stateHover);
}
and even
QLabel#stateIcon::parent::parent:hover {
image: url(:/icons/stateHover);
}
Hoping that it would activate on the hover-state of the parent but it does nothing.
Is there a way to do this purely in stylesheets?
If not, is there a way to propagate the parent hover state to it's children in code?
Maybe with an event handler dealing with the QEvent::HoverMove or QEvent::HoverEnter and QEvent::HoverLeave where you can set the attribute Qt::WA_Hover to your label.
I didn't try it but it should work as HoverEnter and HoverLeave causes update() function to be called.