Problem with selection rectangle - qt

I am trying to make a customized listview in QT for the Symbian OS.
However, I am facing some problems: I am able to draw everything like icons, text etc.. wherever I wish.
However, it is not getting selected by default.
To do selection, I need to draw the rectangle manually.
It could be a problem if they got changed or some other stuff took place, however.
How would I get the default selection?

You probably want to call the setFocus() method which your listview inherits from QWidget.

I need to call the base class paint. Once the overridden function gets over, I need to call the base class paint.

Related

How to replace all cursor appearances within a QML program?

I'm working on a game that uses QML for its UI.
I would like to replace all cursors appearances with cursor-images that are more fitting to the game style (e.g. pointing skeleton hand instead of the normal Qt::ArrowCursor).
Calling QGuiApplication::setOverrideCursor() seams not to be a practical solution as I can not "overwrite" each MouseArea to call may replaceCursor() magic-global-function. For example the change column with cursor within a TableView is currently impossible for me to manipulate.
To me the most practical solution would be to replace the appearance of all cursors but leaf Qt with the tasks to correctly choose the cursor style.
Thanks for any help!
You can still use QGuiApplication::setOverrideCursor() to decorate your mouse areas. It works like a stack, you can set and then restore cursors, so you begin with setting an initial cursor from main.cpp, and then you use an "overloaded" MouseArea which sets its cursor using setOverrideCursor() as well, instead of using the QML functionality.
For example:
onContainsMouseChanged: {
if (containsMouse) Sys.setOverrideCursor(yourCursortype)
else Sys.restoreOverrideCursor()
}
Of course, that means you will have to create an auxiliary object that will call those functions from C++, and expose it to QML so it can be called from there.

How to draw something in a tooltip?

I am trying to develop a functionality using Qt which I don't know whether it is possible to implement. Here is the requirement:
When the user hovers over a node (an object derived from QGraphicsItem), a window will be shown near the node, in the window there might be some histograms or buttons that can be clicked to show further information. When the mouse leaves the window, it will automatically close.
I tried to use a tooltip, because it can pop-up near the node and close when the mouse leaves it, but it can only show text. So, it still cannot work that way. I am wondering if there is another way to do this? I did lots of google search, but there is still no answer.
Thanks so much for helping me with this.
If you're ok with using a 3rd party library, Qxt provides a class that provides a tooltip that is QWidget based, which will let you use an arbitrary widget as the tooltip rather than just text.
See: Qxt::ToolTip
you don't have to use tooltip for your app
you can use or call widget or dialog, on hover mouse event
Please refer Qt Example EmbeddedDialog Example, It is advanced, But you can understand how hover Enter/Leaving events are working. I personally prefer don not create instance of Popupdialog for each item, create it if only nesessary. Otherwise create one dialog and pass its reference to all the items through the constructor initialization.
1. These are the API you are intrested on, reimplemet this.
QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) and void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
2. When You create Dialog, You can pass Qt::WindowFlags as Qt::ToolTip.

Creating Mouse Event for image labels in QT

I am working on an application using QT. I have a mainwindow on which i have an image label. I load an image to this imagelabel and now i want to draw over it. So, my question is- how do i create mouse events for this image label only without having to derive another class of QLabel and instantiating its object to my main window.
My project's due tomorrow and its the only thing i've got left to do. I'd really appreciate the help.
Sincerely, Aayush Shrestha.
If you do not want to create a class that inherits from QLabel to get mouse event, you need to use the installEventFilter function using the eventFilter event of one of your class.

QT drag and drop - Creating a temporary QLabel using qobject_cast

I'm trying to give a custom class that inherits from QLabel to be draggable. Towards that end, I'm trying to create a temporary copy of the class at the current mouse position using the following code inside of the class' mousePressEvent:
QLabel *child = qobject_cast{QLabel*}(this->childAt(event->pos()));
NOTE: this line has carrots instead of brackets, but stack overflow interprets it and takes it out
if (!child)
return;
The child is never created, and I can't figure out why. Any ideas?
If your code is within your QLabel-derived class, childAt() is not the right function. That would return a child widget contained within your label. It doesn't sound like that is what you are trying to do, but correct me if I am misunderstanding.
The object you want to copy is this, but "copy" can have many meanings in c++, and I am not sure exactly you are trying to do. You will probably have to implement it yourself, perhaps with a method called clone() that creates a new instance of your class and populates the values you need to reproduce.
I suspect, though, that there is a better way to implement the drag and drop functionality you are looking for without a copy.

How do I tell Qt to always show an editor in a QTableView?

I've got a QTableView for which I want to display the last column always in edit mode. (It's a QComboBox where the user should be able to always change the value.)
I think I've seen the solution in the Qt documentation, but I can't find it anymore. Is there a simple way of doing it?
I think I could archive this effect by using openPersistentEditor() for every cell, but I'm looking for a better way. (Like specifying it only one time for the whole column.)
One way to get the automatic editing behaviour is to call the view's setEditTriggers() function with the QAbstractItemView::AllEditTriggers value.
To display the contents of a given column in a certain way, take a look at QAbstractItemView::setItemDelegateForColumn(). This will let you specify a custom delegate just for those items that need it. However, it won't automatically create an editor widget for each of them (there could in principle be thousands of them), but you could use the delegate to render each item in a way that makes it look like an editor widget.
There are two possibilities:
Using setIndexWidget, but Trolltech writes:
This function should only be used to
display static content within the
visible area corresponding to an item
of data. If you want to display custom
dynamic content or implement a custom
editor widget, subclass QItemDelegate
instead.
(And it breaks the Model/View pattern…)
Or using a delegate's paint method. But here you have to implement everything like enabled/disabled elements yourself.
The QAbstractItemModel::flags virtual function is called to test if an item is editable (see Qt::ItemIsEditable). Take a look at Making the Model Editable in the Model/View Programming documentation.
I can't see an easy way to do this, but you might be able to manage by using a delegate. I honestly don't know exactly how it would work, but you should be able to get something working if you try hard enough. If you get a proper delegate, you should be able to set it on a whole view, one cell of a view, or just a column or row.

Resources