QT 4.7.4 : need a signal when qsplitter is collapsed - qt

I am somewhat new to qt and working on an already written code.
I am trying to do something whenever a specific widget in my Qsplitter is collapsed.
for this I need a signal from qsplitter or qsplitterhandle.

A QSplitter will emit the signal splitterMoved(int pos, int index) when it is resized. You should be able to identify the splitter you are interested in with index and then look at the value of pos to determine if it is collapsed.

Related

Qt paint on a widget created by QT Designer

How can I paint on a widget without overwriting the paintEvent.
I want to paint on a widget which is inside another one generated by Qt Designer , so i can't overwrite its paintEvent.
I tried to paint directly like this :
QPainter *painter= new QPainter(ui->drawArea);
painter.drawLine(50,50,50,150);
painter.close();
but Qt tell me that the QPainDevice is 0 or something like this,
I tried the same example by creating the painter then call the begin() method with the QPaintDevice (the widget) but same problem.
Qt version : 4.8.6.
Using custom widgets in Designer is not an issue. In Designer, add your widget as any other QWidget or QPushButton, depending which has the closest inheritance. Then with right click menu select Promote to ..., add your MyWidget.h and then promote the widget to MyWidget with reimplemented paintEvent(). Read more:
http://doc-snapshots.qt.io/4.8/designer-using-custom-widgets.html#promoting-widgets

Parenting an object in PyQt causes the object to be owned by Qt instead of PyQt. What are the consequences?

In the QObject.__init__(self, Parent=None) documentation it states:
The parent argument, if not None, causes self to be owned by Qt instead of PyQt.
What does it mean to be owned by Qt instead of PyQt? Does this have effects on behavior that I should be aware of when developing a PyQt application?
Yes, It is. It have Effects to before create any object in PyQt application. If we set parent QWidget None, I can be tell this QWidget is top-level window. I will try explain;
"Parent Widget equivalent like frame of windows"
Parent Widget of QMainWindow is none. Then, owned by Qt instead of PyQt (or easy word root). Parent Widget of QWidget (in red) is QMainWindow, Then geometry reference by QMainWindow. Parent Widget of QPushButton is QWidget (Green), Then geometry reference by QWidget (Green). And all widget as same ...
So, QMainWindow have fully control widget in your child. For example, If self delete, All child will follow delete too. etc.
But, If we set Parent Widget of QWidget (in red) is None, that mean this QWidget like new window. Like this picture;
For now, we have 2 top-level window. But QWidget have independent with QMainWindow. If self delete, All child will follow delete too but not outside QMainWindow. Outside QWidget not delete. So, some application have create many widget, but it will has main widget in main menu to control child.
Not only frame of windows, It just Example & Comparison.
Regards,

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

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.

Qt - How to improve my contextmenu

with help of visitors of this forum I have made a custom context menu in QListWidget. Now Im trying to solve a problem that the menu is showing up when the user have rightclicked on the widget but the mouse wasnt over any item. When this situation occurs I want the menu to not show up.. Ive been thinking about obvious condition - is the mouse over some item in the widget? But I dont know how to do this.
thank you in advance for all the answers :)
Since you do not show how you are currently creating your context menu, I will assume from the beginning...
Set your widget's contextMenuPolicy to Qt::CustomContextMenu
Connect the widgets customContextMenuRequested signal to a slot that will show your custom menu
The slot will receive a QPoint in local widget coordinates. You can use this QPoint to find out which item is under that point with QListWidget.itemAt(QPoint). You can then validate that pointer. If it's a valid item, show your menu at that point (or some offset of it that suits you). A valid item means a non-null pointer.
Here is an example of what it looks like in PyQt. You can translate this to Qt
class Widget(QtGui.QWidget):
def __init__(self,parent=None):
super(Widget, self).__init__(parent)
self.list = QtGui.QListWidget()
self.list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.list.customContextMenuRequested.connect(self.handleContext)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.list)
self.list.addItems(["one", "two"])
def handleContext(self, pos):
item = self.list.itemAt(pos)
if item is not None:
menu = QtGui.QMenu("Context Menu", self)
menu.addAction("FOO")
ret = menu.exec_(self.list.mapToGlobal(pos))
In PyQt4, if the item at the given position is None, then it was an empty space. In Qt, it should be a null pointer.

Qt : execute a slot each time a QTableWidget change its geometry?

I've made a widget derived from QTableWidget and I would like to execute a slot each time the a column or a line is resize, and each time the widget is resized. How to do that as there is no signal for that ?
Thank you very much.
For resizing of the widget itself: Inherit from QTableView, add a signal reimplement resizeEvent. In your resizeEvent emmit your new signal and call the base implementation of resizeEvent to actually do the resizing.
For resizing of columns: Use the signals from the associated QHeaderView which you can obtain with horizontalHeader

Resources