Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. workaround to showing a widget when replacing hidden widget in splitter
Forum Updated to NodeBB v4.3 + New Features

workaround to showing a widget when replacing hidden widget in splitter

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 629 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have a splitter where i insert widgets which might later need to be replaced.
    when the splitter has a widget which is hidden, then when i replace the widget, the new widget is hidden too (from docs: The geometry of the newly inserted widget will be the same as the widget it replaces. Its visible and collapsed states are also inherited.)

    so i wanna know how i can do the widget replacement to get the newly inserted widget displayed regardless of the previous widget's visibility.

    sample code:

    #include <QSplitter>
    #include <QLineEdit>
    #include <QComboBox>
    #include <QPushButton>
    
    class WidgetContainer : public QWidget
    {
    	Q_OBJECT
    public:
    	WidgetContainer() : m_pSplitter(new QSplitter(Qt::Horizontal, this))
    	{
    	}
    
    	void setWidget(QWidget *widget)
    	{
    		if (m_pSplitter->widget(0))
    			m_pSplitter->replaceWidget(0, widget);
    		else
    			m_pSplitter->insertWidget(0, widget);
    	}
    
    public slots:
    	void addWidget()
    	{
    		setWidget(new QComboBox);
    		show();
    	}
    
    private:
    	QSplitter *m_pSplitter = nullptr;
    };
    
    int main(int argc, char *argv[])
    {
    	QApplication app(argc, argv);
    	WidgetContainer window;
    
    	auto pLineEdit = new QLineEdit;
    	pLineEdit->hide(); // note: initial widget is hidden
    	window.setWidget(pLineEdit);
    
    	auto pBtn = new QPushButton(&window);
    
    	QObject::connect(pBtn, &QPushButton::clicked, &window, &WidgetContainer::addWidget);
    
    	window.show();
    
    	return app.exec();
    }
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      In that case, why not remove the old one and insert the new one ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      U 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        In that case, why not remove the old one and insert the new one ?

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @SGaist
        yeah but how do i remove it?
        when i tried to get splitter's layout it was null

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          My bad, there's indeed no remove method. I was on the wrong page of the documentation.

          One thing you can try is to reparent the widget and then insert the new one.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          U 1 Reply Last reply
          0
          • SGaistS SGaist

            My bad, there's indeed no remove method. I was on the wrong page of the documentation.

            One thing you can try is to reparent the widget and then insert the new one.

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @SGaist
            i don't wanna re-parent cause i don't own the widget.
            i just hided the previous widget and inserted the new one

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved