Get object handler user clicked in scene - qt

QList<QWidget *> list;
In scene user can add no of objects and m storing objects in list. i want to get the object the user clicked or focused one so that i can change properties or delete object. how to get clicked object pointer from list ?

QWidget might not have the signals and methods you need to implement this easily.
Look here http://doc.qt.io/qt-5/widget-classes.html to find the widget best suited for your needs.
In order to keep track for the signals and to which button they belong you can want use a QSignalMapper.

Related

What is the life time of object passed as receiver for a QAction in a QMenu widget?

I'm working on QMenu to implement some custom functionality.
I'm having trouble in understanding when does a particular object gets destroyed during the execution of QMenu pop and triggering of any action.
here is the snippet of the actual codde :
myCustomMenu, a derived class from QMenu has a function customExec()
void myCustomMenu::customExec()
{ myCustomMenu *rearrangedMenu = new myCustomMenu();
myCustomFunctionalityClass *mCFC = new mCFS(this, rearrangedMenu);
//This class saves "this" as originalMenu i.e. the current menu object's address in a variable so that I can access with following action's SLOT.
rearrangedMenu->addAction(" Open My Custom Widget", mCFC, SLOT(showWidget()) ;
rearrangedMenu->exec();
}
When I click on "Open My Custom Widget" a gui widget opens up, and inside the showWidget() if I try to access mCFC->originalMenu->size() it crashes.
It appears that the object has been destroyed.
My problem is that how can I access that object? And if it gets destroyed then why not mCFC object which is also a local variable.
EDIT: showWidget slot is a member of mCFC class.
Please add comments if you have any doubt understanding it. I'm stuck on this since a while.

Qt: make view to update visible data

In my program I use QTableView and QAbstractTableModel that are connected. Model doesn't contain data. When view needs data to show it calls QAbstractTableModel::data and model uses another object to get data and return. At some point data in that object is going changed. Model doesn't know what has changed so dataChanged is not called.
I need that only visible part of data (that is shown in view) goes updated. It should get new data from model. I am trying to achieve that by calling update() or repaint() functions of view but it doesn't help. I am thinking that it should call paintEvent of tableview but it is not called.
How is it possible to make view update visible part of data? I don't want to update whole data that is huge.
Your wishes brokes Qt MVC logic. But if you need workaround - you may do next call to update visible area: emit dataChanged( QModelIndex(), QModelIndex() );

Accessing methods from another class inside a QWidget

I have a class/ QDialog (let's call it "Frame") that contains other classes. This is what it looks like:
In the above screenshot, everything inside the green rectangle is actually a separate class/custom QWidget (let's call it "Page3" since it is the third item in the list) placed inside a QStackedWidget while everything outside the rectangle is part of Frame. Everything inside the rectangle is therefore separate from Frame even though it appears to be part of the same form. Clicking the Overview and SQLite Journal objects causes a separate page to load inside the QStackedWidget. All of these classes must be able to communicate with each other.
The problem is, I’m not sure how to access Frame's public methods or variables from inside Page3. See, one of the functions of Page3 is to unlock the OK button in Frame when the contents of the two password fields (Password & Repeat) match. To do this, Page3 needs to call the method that unlocks the buttons in Frame. I need to communicate with the currently-running instance of Frame instead of creating a new copy so instantiating Frame from inside Page3 doesn't work. I can't use parent() either because that simply refers to the QStackedWidget inside Frame instead of Frame itself.
I'd appreciate it if someone could tell me how to do this.
I prefer to do this kind of thing (communication between a child widget and its parent) using signals and slots. Why? Because if the child depends explicitly on the parent, you end up with a circular dependency, and it's harder to change your design in the future.
The simplest solution is to have Page3 emit an "unlockOk" signal which is connected to a slot in Frame which does the actual "unlocking" of the button. Frame could connect the signal and slot in its constructor, or wherever else it's actually instantiating the Page3 object.
If you want to take it a step further, you could make the signals more generic; for example, signals called "inputValid" (which would be emitted when the password fields match) and "inputInvalid", which would be connected to "unlockOk" and "lockOk" slots. The reason for doing this is that you could re-use the signals in other parts of your application if you need to, and their names clearly indicate what they're communicating.

Populating a QComboBox and a QTable[View|Widget] from javascript

I'm trying to write a script for an application developed with Qt, using javascript for the business logic and a .ui file for the GUI, but I'm facing two problems.
In the ui I declared a QComboBox, to which I successfully connect javascript functions to handle
signals such as editTextChanged, etc. I was wondering I cannot populate the combobox from within
javascript code, because the addItem function is not exposed to script-side code.
combobox.editTextChanged[action](ComboBoxChanged); // OK (action is "connect" or "disconnect")
combobox.addItem("element 1"); // Error!
Is there any (other) way to do this?
I need to show a set of items (strings) in a table-like component. I tried using a QTableView and
QTableWidget but I cannot insert or get items. For example, from javascript I cannot access the
setModel function of a QTableView (if at least I could create a QAbstractItemModel from
script...), neither I can access the item(row,col) function of a QTableWidget class, to set an
item's text. Is there any way to show a table of strings to the user, let edit them and retrieve
the modified contents?
Thanks in advance.
Antonio
Because the addItem() function isn't a slot, you'll need an intermediate public slot to handle the transaction. It'll be the same with the other functions you are trying to get at as well.

Qt4 QMenu items sorting

I am using QT4 and dynamically adding entries to a QMenu. Is it possible to sort the entries in the QMenu without deleting it and creating a new one?
I originally thought there was a function to insert at a specific location so I could sort on insert, but I have not been able to locate it.
Once added, I don't think you can reorder. While you are creating though you could use the QWidget::insertAction method to place it exactly where you want it.
void QWidget::insertAction ( QAction * before, QAction * action )
Otherwise you could use QWidget::addActions. Create your list of Actions and sort it before adding to the QMenu.
void QWidget::addActions ( QList<QAction *> actions )
In one of my codes, I save the QActions into a separate List and generate the menus and submenus on demand. In theory, I can add "weight" to the items and have them re-ordered, but I have not implemented this yet.
Project page is available here: http://code.google.com/p/qtedit4/wiki/qmdilib
Please note that the actions of QWidget (and QMenu) are stored as a QList which can be "read", using QWidget::actions() . Remember that the list is copied, so you can modify the actions but not the list itself. (I hope I am not mistaking...)

Resources