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);
Related
I have a scrollarea and I'm setting a basic QWidget as its widget, However when I do this I get a toolbar on top of the widget I just set;
This is the slot I use to create the QWidget and set it to the scroll area, this happens during runtime and the qwidget is deleted and recreated repeatedly during runtime.
void SlotCreateDetailsWidget()
{
if (m_DetailsWidget == nullptr)
{
m_DetailsWidget = new DetailsWidget(); // QWidget
m_Ui->DetailsScrollArea->setWidget(m_DetailsWidget);
m_DetailsWidget->show();
}
}
What do I need to do to get rid of this toolbar?
From https://doc.qt.io/qt-5/qwidget.html :
A widget without a parent widget is always an independent window
(top-level widget).
What you see depends on the underlying windowing system but you can customize it up to a certain point with void QWidget::setWindowFlag(Qt::WindowType flag, bool on = true) for example. You can also look at https://doc.qt.io/qt-5/qtwidgets-widgets-windowflags-example.html.
Click the button, a table control appears next to the button, click the blank space or the content in the control to close.
I have two ideas:
With the popmenu, can click the blank space or the content in the control to close the control, but it does not meet my needs for a table-type control
New a tableview, but I don’t know how to accurately appear under the button and realize the function of clicking the blank space or the content in the control to close the control
Update:
I need to use Qt to implement a function, but I don't know how to implement it.
The functions are as follows:
Click a QPushButton, and a QTableView is displayed below the button. I can click anywhere outside the QTableView to close this QTableView
There are two things I cannot achieve:
1.How to display the QTableView below the QPushButton (must be immediately below, such as:
(source: upload.cc)
)
2.How to close the QTableView by clicking the blank space outside the QTableView
If you can make all of this in the same window, it's simple...
To position the QTableView below the QPushButton you will use Qt Designer, and setup the QTableView to hide();. And when the QPushButton is clicked, he setup the QTableView to show();.
An show(); example:
QTableView *nametable = ui->tableView;
nametable->show(); //will appears
But to close or hide(); this when clicked in a blank space I don't know how to help you...
I’ve created a custom model, view (tree) and delegate and everything seems to be working OK with one minor issue. Some of the cells of the tree are combo boxes. The combo boxes work as expected when selected by the user (custom createEditor, setEditorData and setModelData delegate methods). The issue I have is the down arrow is only displayed when the cell is selected. I would like the down arrow to be displayed all the time.
I’m not sure which method is control’s the display of the down arrow. Does it come from the model data (maybe the decorationrole) or is it the paint method of the delegate. Note I’m not using any style sheets.
Update:
Found the solution I was after here. Basically ended up with the following in the paint method of my custom delegate.
QStyleOptionComboBox comboBoxOption;
comboBoxOption.rect = option.rect;
comboBoxOption.state = option.state;
comboBoxOption.state |= QStyle::State_Enabled;
comboBoxOption.editable = false;
comboBoxOption.currentText = index.data(Qt::DisplayRole).toString();
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
When QTreeItemView show own items. It's use QStyledItemDelegate for show content. When you edit cells, creates real QCombobox widget.
So, editing and showing items have different approaches.
You should change QStyledItemDelegate::paint logic for see additional decorations when cells just showing (painting).
Make children of QStyledItemDelegate and define own paint() method.
That will not simple for first time. But that true way to implement "arrow" in cells.
I have a form written with Qt Designer, which contains a QWidget promoted to a custom widget I have. The custom widget contains several combo boxes. I want the form to have a reasonable tab order, with focus moving from the widget immediately before the custom widget, then going through the combo boxes in the custom widget, and proceeding to the widget after the custom widget. So I set the QWidget to have tab focus in Designer, but the custom widget doesn't handle having focus properly.
I could solve this problem using QWidget::setTabOrder, but that would be messy because I would have to reach into the custom widget from the outside. Alternatively I could give the custom widget a member function to set the tab order. Ideally there should be a simpler way. Is there?
I have one question regarding the QPushButton.
i want the QPushButton behaviour in such a way that it should be shown only when the focus is there on QPushButton, and when the focus is out then it should hide. Below is the image that have "View" button, it displayed only when the focus is there on the QPushButton.
Thanks,
Neel
Subclass QWidget.
Create a QPushButton member.
Override the QWidget::enterEvent and QWidget::leaveEvent protected methods to show / hide the QPushButton.
Override the QWidget::resizeEvent to resize the QPushButton.