Important: Please read the Qt Code of Conduct -

[SOLVED] QList<CustomWidget*> : how to get a handle to its contents

  • I have a class CustoWidget which subclasses QWidget

    I need multiple of it so I stored them inside QList:
    @QList<CustomWidget*> manager;@

    Inside "function1()", I initialized each CustomWidget everytime I call this function and add them to the list
    @CustomWidget *widget = new CustoWidget();

    Within "function2()", I need to retrieve all CustomWidget in the list to reposition them:
    @CustomWidget *widget;
    widget->move(x,y); // x and y were retrieved using QDesktopWidget and doesn't matter in the code

    but after function2() is called the CustomWidgets disappears but they are still inside the QList.

    Note: The CustomWidgets are small non-modal sub-windows similar to QDialog but with specific purpose

    I can't use signal and slot since they will stack up when there are multiple CustomWidgets and I think its impractical to track them when some were closed by the user;

  • Moderators

    That is a strange setup, but this is not the question.
    foreach (CustomWidget *w, manager) {
    this is the standard way of using foreach. Maybe x, and y are too big and place your widgets outside the visible region of desktop?

  • I don't seem to grasp your actual problem.

    So your widgets disappear after <code>widget->move(x, y)</code>? You most probably moved then to a non-visible area then (out of screen). Use <code>qDebug() << x << y;</code> within your loop (don't forget to <code>#include <QtDebug></code>) to find out where they are actually moved to.

  • I don't really understand your problem, so:
    the problem is the disappear? if so don't they disappear because x,y point is outside of screen?

    or answer to the question in the title is you can use one of QList's members: "operator[]": or "value()": or "at()": example: manager.value(0)->customWidgetfunctionality();

  • Thanks sierdzio and Lukas Geyer and Zlatomir!

    It works now. I also got the question in my mind that I know I am doing the right thing, why it isn't working?

    Apparently, x and y became "negative" so they became invisible. There is just a small problem with computing for the x's and y's of each CustomWidget. I always used QDebug in all my codes, I just missed "x and y" this time. My Bad!

    Thanks! I appreciate your help.

Log in to reply