How to detect if focus shift from a widget inside a QWidget to one outside? - qt

I'm programming in Python using Qt with PySide and I have custom QWidget defined in a file called editor.py which is inserted in my ui in windowUi.py using the promotion method in the Qt Designer.
The custom QWidget class defined in editor.py doesn't do much besides using Elixir to edit items in a sqlite3 database and importing a ui file (editor.ui). Inside editor.ui there are a couple of QLineEdits and QDateTime widgets.
This widget is originally hidden in the main window and showed when needed. So far so good, but the problem is that I cannot make it hide when not needed. I decided that the widget is not needed when the user clicks anywhere else in the main window that is not the editor widget imported, that is, focus shift from the QWidget.
I looked upon this question: QWidget focusOutEvent not received and realized that the QWidget is really not getting focus.
If I call setFocusPolicy(StrongFocus) on it then I can make it hide if, and only if, the user clicks on the QWidget background (not on any widget inside it) and then clicks outside.
The question is then, how can I make it such that when the user clicks outside of this widget, shifting focus from any QLineEdit or QDateTime that's inside it to something else, the QWidget then hides itself?

Doesn't QApplication:::focusChanged ( QWidget * old, QWidget * now ) do what you need? You can check if QWidget *now is one of your QLineEdits/QDateTime or not (f.e. by going up by the QObject::parent)
Simply connect to this signal before showing and disconnect after hiding.

Related

Can a QToolbar be added to a QDockWidget?

I have setup my app to have various dock windows within the main window. I am also able to add a toolbar to the main window. However, I would ideally like to add the QToolBar inside one of the QDockWindow instances (or the QWidget that it houses) as the toolbar will be specific to that window.
Is this possible? I'm using a recent version of Qt, 5.10.
I think it is possible.
1.QDockWidget can set a QMainWindow by setWidget() method.
QMainWindow is made for just a mainwindow but it is not prevented from being used as a subwidget.
2.QToolBar can be attached to the main-subwindow by addToolBar() method.
3.The subwidget-mainwindow can naturally have its own QToolbar.
If you don't want to use QMainWindow as the widget of its QDockWidget,you can attach the QToolBar as a child widget of QDockWidget. But The toolbar is not movable as QMainWindow's.
I think you want to add QToolBar and use it as QMainWindow.
So I recommend that you set a QMainWindow as the widget of QDockWidget.And you attach any widget you like to the mainwindow after that.

How to set cursor when it is in some area?

I have some rectangle link area on my widget. What is the best way to make cursor Qt::PointingHandCursor when it is in this area?
The QWidget class has a cursor property that you can set with the cursor you wish displayed when the mouse is above it.
EDIT:
Without more detail on what you are trying to achieve, I can only assume you're making your life much more difficult than it needs to be. You can create a QLabel widget to handle the link and then place the label on the menubar automatically.
QLabel *link = new QLabel("<a href='http://doc.qt.io'>Qt Documentation</a>");
menuBar()->setCornerWidget(link);
All the text formatting, cursor display and user interactions are handled by existing code in the Qt classes. The only thing you need to do yourself is to handle what happens when the user clicks on the link, that you can do by connecting a slot to the QLabel::linkActivated(const QString &) signal.

QStackedWidget navigation from page to page

I think i'm having a fairly basic Qt problem, but i can't figure it out:
I have a QMainWindow which holds a QStackedWidget. All page widgets in there are seperate classes derived from QWidget.
So:
QMainWindow implements QStacked Window in one class.
All other pages inside the stacked widget are added classes and all have there own .ui filled with buttons and lists trough the Designer.
For navigating to different pages, inside the Mainwindow i have access to: ui.stackedWidget->setCurrentIndex(2);
It seems i don't have access to ui.stackedWidget on another page inside the stacked widget? I have no access to the ui.stackedWidget because Ui is a private member in the mainwindow class. (auto generated by Qt - using VS addon for adding QT4 classes)
I would like to know, how can i jump to another page in the stacked widget, when clicking on a button that belongs to another page inside this widget?
Note:
All pages are added to the StackedWidget in mainWIndow's constructor:
ui.stackedWidget->addWidget(page1Widget);
ui.stackedWidget->addWidget(page2Widget);
// etc..
Example of a button click signal-slot inside page1Widget:
connect(ui.btnViewData, SIGNAL(clicked()), this, SLOT(viewData()));
::viewData()
{
// navigate to another page here.
// note: ui.stackedWidget->setCurrentIndex(3); is not accessible here!
}
I believe that putting your connect() and viewData() functions within your QMainWindow object will solve your problem, since the main window can have access to both the signals emited by the child widgets and the QStackedWidget items.
You might need to write a Ui getter for each of your page, and then do something like
connect(page1Widget->getUi().btnViewData, SIGNAL(clicked()), this, SLOT(viewData)));
hope it helps,
cheers

QAction vs QToolButton and when to override the Basic class?

I've recently been studying Qt, and have the following questions:
What is the difference between QAction and QToolButton?
How do I know when to override QPushButton? For example, should I override in order to be informed when the mouse enters a QPushButton's bounds? I don't need to in order to get the signal click().
Question 1:
QActions are used to define tasks performed by an application in a way which can be understood by a number of different user interface objects. Using an example from the Qt docs for context:
A QAction is defined; it is given an icon image, a text description, a keyboard shortcut and a longer tooltip description, as well as being linked to a user defined function.
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
Later in the implementation, the QAction is added both to a textual menu bar...
fileMenu->addAction(newAct);
... and an icon-based tool bar:
fileToolBar->addAction(newAct);
The menu bar uses the text description to create an entry in the menu corresponding to the action. Similarly the toolbar uses the icon set on the QAction to create an icon in the toolbar which corresponds to the action. Selecting the menu item, clicking the toolbar icon or pressing the keyboard shortcut will all result in the same effect: that defined by the linking of QAction::triggered() to newFile().
So to directly answer your question: a QAction is an abstract way of defining the various parameters and behaviour of a particular task performed by the application. A QToolbarButton is a UI object (derived from QWidget) created by a QToolbar in response to QToolbar::addAction()
Question 2:
Yes, QPushButton has a clicked() signal inherited from QAbstractButton, but it does indeed lack a way to inform when the mouse has entered its bounds. You have a couple of options in order achieve this, but first you need to first set the mouseTracking property to be enabled. This will allow you to receive mouse move events on the QPushButton even if no mouse buttons are pressed. With that done you need to explore one of the following options:
As you suggest, you could subclass QPushButton and reimplement mousePressEvent in order to respond to the mouse position.
You could install another widget as an eventFilter on the QPushButton, and watch for events of type (QEvent::MouseMove).

How to remove focus from a QLineEdit when anyplace else on the window is clicked

I'm working on a custom Qt button that allows you to edit the text on the button if you double click it. When the button is double clicked, a QLineEdit appears where the text on the button is allowing the user to edit the text on the button. My requirement is that if the user clicks anywhere in the application window, the QLineEdit should disappear and cancel the edit operation. This works in some cases. Specifically, it works if I click on anything that is capable of text entry. Other portions of the window don't work as expected. I'll click on a blank portion of the application window, and the QLineEdit retains its focus. How can I remove its focus in these cases?
I've found a solution that seems to work, though I'm still open to other options if there are any. I'm using PyQt4, so my example is in python:
Create a subclass of QLineEdit just so I have a new type. I don't want or need this behavior on all QLineEdit instances; just these specific ones.
class MyLineEdit(QtGui.QLineEdit):
pass
Now, in my QMainWindow subclass, I override the mousePressEvent() implementation. It gets the currently focused widget. If that widget is of type MyLineEdit, clear the focus.
class MyMainWindow(QtGui.QMainWindow):
def ...
def mousePressEvent(self, event):
focused_widget = QtGui.QApplication.focusWidget()
if isinstance(focused_widget, MyLineEdit):
focused_widget.clearFocus()
QtGui.QMainWindow.mousePressEvent(self, event)
def ...
This gets me the behavior I'm looking for so that if the user clicks anywhere on the application's window, the focus is cleared.
Edit: I did find one caveat to this. I have a QTreeView in the main window. If the user clicks on the tree view, focus is not removed from the text edit field.
Catch the clicked() signal of your parent widget and call yourLabel->clearFocus() (that unfortunatelly happens to not be a slot, making things more complicated) there.
I followed Grant Limberg instruction here but figured out that, in my case, a simple:
QApplication.focusWidget().clearFocus()
would fix the problem.
I'm not sure if this also works in Qt4 (I'm using PyQt5) but you can change the FocusPolicy of the QMainWindow or parent widget to clear the focus in the QLineEdit. As per https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop
I've changed the policy of my QMainWindow to Qt.StrongFocus and it worked like the functionality described in the question.
If done in C++ I would do something along the lines of:
connect(myWidgets->MyLineEdit, SIGNAL(returnPressed()), this, SLOT(onLineEditDone());
void onLineEditDone()
{
myWidgets->MyLineEdit->clearFocus();
}
For this particular case I would use editingFinished() instead of returnPressed(), probably, but I would NOT use textChanged(QString).

Resources