[Solved] Replace inner widget in QDockWidget...



  • I have QDockWidget.
    I have a QTableView and a QListView.
    In QDockWidget I can set the widget with this: setWidget(widget*). Till now ok.
    But!
    Later, I'd like to replace the table with the list
    (I have a combo in the header of dockwidget (for the signal/slot) where could I change between the two).
    Unfortunately, my views clashes and crashes :(
    How can I programatically switch between the two views "in runtime"?



  • You can provide the implementation code here for review.



  • Relevant codesnippets:

    @
    VC2DockWidget::VC2DockWidget(QDockWidget* parent) : QDockWidget(parent)
    {

    // dockwidget titlebar
    w = new QWidget(this);
    hbl = new QHBoxLayout(w);
    cb = new QComboBox(this);
    cb->addItems(QStringList() << "Table" << "Icon");
    le = new QLineEdit(this);
    hbl->addWidget(cb);
    hbl->addWidget(le);
    w->setLayout(hbl);
    setTitleBarWidget(w);
    
    etablew = new EventWidget(this);
    elistw = new EventListWidget(this);
    
    setWidget(etablew);
    
    connect(cb, SIGNAL(currentIndexChanged(int)), this, SLOT(changeView(int)));
    

    }

    void VC2DockWidget::changeView(int comboindex)
    {
    if (comboindex == TABLE)
    {
    setWidget(elistw);
    }
    else
    {
    setWidget(etablew);
    }
    }
    @

    @
    EventWidget::EventWidget(QWidget *parent) : QWidget(parent)
    {
    vblayout = new QVBoxLayout;
    QString databaseName = ":/Database/event_table.csv";
    db = new Database(databaseName, this);
    eventModel = db->getModel();
    eventTable = new QTableView(this);
    eventTable->setModel(eventModel);
    vblayout->addWidget(eventTable);
    setLayout(vblayout);
    }
    @

    (EventListWidget is almost the same than the EventWidget. Contains QListView instead of QTableView)



  • Hehe! Solved!

    @
    VC2DockWidget::VC2DockWidget(QDockWidget* parent) : QDockWidget(parent)
    {
    // dockwidget titlebar
    w = new QWidget(this);
    hbl = new QHBoxLayout(w);
    cb = new QComboBox(this);
    cb->addItems(QStringList() << "Table" << "Icon");
    le = new QLineEdit(this);
    hbl->addWidget(cb);
    hbl->addWidget(le);
    w->setLayout(hbl);
    setTitleBarWidget(w);

    etablew = new EventWidget(this);
    
    setWidget(etablew);
    
    connect(cb, SIGNAL(currentIndexChanged(int)), this, SLOT(changeView(int)));
    

    }

    void VC2DockWidget::changeView(int comboindex)
    {
    QWidget* w = widget();

    if (EventWidget* ew = qobject_cast<EventWidget*>(w))
    {
        setWidget(new EventListWidget(this));
    }
    else
    {
        setWidget(new EventWidget(this));
    }
    

    }
    @

    I hope, this is not a "brutal" solution :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.