How to get the id of a window in qml? - qt

I want to get the id of a specific qml window in a cpp class, but I don't know how.
something like:
int id = testWindow.id()
The id that I'm talking about is the id of the window in the system.
How can I do this without change the main.cpp?

The QQuickWindow inherits QWindow which has the member
WId QWindow::winId() const
Documentation: https://doc.qt.io/qt-5/qwindow.html#winId

Related

Open Existing QMainWindow from QMainWindow

I have a little question - I have QMainWindow with button. On click on that button I would like an existing QMainWindow to open (detail: I want open a window with QWT Plot. I mean the refreshtest project example of QWt.)
So I would like to existing Window open on button. As far I was able to open new QMainWindow, but what can I do to see MainWindow from project refreshtest?
void MyWindow::on_pushButton_1_clicked ()
{
QMainWindow *mw = new QMainWindow();
mw->show();
}
With this code, you will get a leaking pointer after leaving the scope of the method.
The reason is that the show() method is not blocking. It will post an event into the event loop queue. It will get processed in an async manner whenever the Qt event loop "gets the capacity for that".
You have several options to address this issue.
1) Qt parent/child relation
QMainWindow *mw = new QMainWindow(this);
^^^^
2) Make "mw" a class member of MyWindow
m_mw.show();
and construct it in the MyWindow constructor.
3) Use a smart pointer
QSharedPointer<QMainWindow> mw = QSharedPointer<QMainWindow>(new QMainWindow());

how to set accessible name for a QGraphicsScene?

I'm using QT. I want to set accessible name for a QGraphicsScene because at the mouse click I want to check whether witch graphics scene has been clicked?
Thank You
Each class that is derived from QObject(including QGraphicsScene) has following function to set a name for it:
void setObjectName ( const QString & name )
And you can retrieve this name with this function:
QString objectName () const

How to access the QListWidget or QListWidgetItem from a QAbstractItemDelegate

Is this even possible in Qt? I have a QListWidget setup with a delegate for specific painting, and I'm attempting to have the delegate paint differently based on variables in my QListWidget's parent. I figure I can rig something up as long as I have a pointer to either, but I need to somehow get them while in the delegate's paint().
I've tried the "hack" to store a pointer in a QVariant, but it doesn't seem to be working, and I'd rather not take this approach. If I could get to the pointer to item the delegate represents, and just call item->listWidget(), this would be perfect, but it doesn't seem possible while inside the delegate.
Is there any workaround to accomplish this?
Also, just because.. sample of trying to get the void* trick to work - if this is the only way to do this, perhaps someone may see what I have wrong in it.
//Parent of QListWidget
....
QListWidgetItem *item = new QListWidgetItem();
....
QVariant v = qVariantFromValue((void *) pStitchSymbolCustom);
item->setData(Qt::UserRole + 6, v);
....
//Implemented QAbstractItemDelegate
....
MyClass* p_pointer =
(MyClass*)(index.data(Qt::UserRole + 6).value<void *>());
I'm not sure, if i understood your question correctly, but if you want to access the QListWidget wich contains your delegate, then i think, that the easiest way is to set the parent of the delegate as the listwidget, and then get the listwidget at any point of the delegate:
QAbstractItemDelegate * delegate = new MyItemDelegate(myListWidget);
myListWidget.setItemDelegate(delegate);
and then in the delegate code:
QListWidget * listWidget = qobject_cast<QListWidget*>(parent());
//You can do whatever you want your list here
If your object tree is deep, and you want the code to be generic and not care about where the desired parent is, you should ascend the tree automatically:
QListWidget * listWidget = 0;
QObject * object = parent();
while (object && ! listWidget) {
// qobject_cast will succeed once the parent of the correct type is reached
listWidget = qobject_cast<QListWidget*>(object);
object = object->parent();
}

Adding widget to layout specified in QString

I started my first steps programming in Qt and can't find an answer.
I have some widget, let's say it's named "tab".
I want to add to it's layout new widget.
I can do it by writing:
QLabel *label = new QLabel(tab);
I want to do the same thing , but instead of saying "tab" I want to use widget name stored in QString variable. Something like this (but it doesn't work, can't convert QString to QWidget):
QString name = "tab";
QLabel *label = new label(zakladka);
Is it possible?
QObjects have a property objectName. So if you name your objects and have them be part of a hierarchy of QObject (i.e. you know they are children of a parent object) you can make use of findChild
QLabel* label = new QLabel(parentWidget->findChild(name));

ComboBox Qt Creator - how to create an event?

I have a Qt Form that contains 2 combo box messages. The second combobox message depends on the first combo box message. I mean that the dates from the second combobox message depends on the element that I select in the first combobox.
In this moment I have different dates in the first combobox. But the second combobox is not working. I need to creare a connect method or what?
Thx! APpreciate!
Could someone give me a short example?
It's fairly simple. A combobox emits the currentIndexChanged signal that also tells you the new index. Write a method that accepts an integer and changes the second combobox according to the integer (which is the index of the selection in combobox 1).
Here are some code sniplets from a working example.
Method declaration in your window/whatever class header:
public slots:
void setI1(int index);
Filling combobox 1, connecting the signal, e.g. in the constructor:
i1Box->addItem("Neutral", 0);
i1Box->addItem("2,856 K (Illuminant A, light bulb)", 2856);
// ...
connect(i1Box, SIGNAL(currentIndexChanged(int)),
this, SLOT(setI1(int)));
Implementation of the method:
void ViewerWindow::setI1(int index) {
// either use index directly, or, as in this case we have items holding an int:
int i1 = i1Box->itemData(index).value<int>();
// use the value to change second combobox here
}
If it does not work as expected, it is always helpful to print some debug output inside the method that should be called to see where it goes wrong in the chain.
Reference: http://doc.qt.nokia.com/latest/signalsandslots.html

Resources