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. Add widget in code
Forum Updated to NodeBB v4.3 + New Features

Add widget in code

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 510 Views 3 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    I can successfully add new tab and insert a sub tab into it.
    I do not see a function to add / insert ANY widgets to the sub tab, just another tab .

    void Form_Widget_HCI::on_lineEdit_2_returnPressed()
    {
        // create new tab 
        QTabWidget *tab = new QTabWidget();
        ui->tabWidget->addTab(tab, "New tab test");
        // create subtab
         QTabWidget *tab_sub = new QTabWidget();
         // create widget 
         QTextEdit *text = new QTextEdit();
        tab->addTab(tab_sub,"Test subtab");
    
        **// add widget to subtab ???** 
        ?? 
        
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      set a layout for the tab and add widgets to the layout.

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

        Hi,

        tab_sub->addTab(text, tr("Text Edit"));
        

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

        1 Reply Last reply
        4
        • M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #4

          Off course, we have to create page widget in the first place.
          The code above only creates tabwidgets, confusing :)

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #5

            @SGaist said in Add widget in code:

            tab_sub->addTab(text, tr("Text Edit"));

            Ok, this actually creates / adds a tab , which is nice.
            Where do I look for "tr" description ?

            Here is partially what I have

            // create new (main) tab
            QTabWidget *tab = new QTabWidget();
            ui->tabWidget->addTab(tab, "New tab test");
            // create subtab
            QTabWidget *tab_sub = new QTabWidget();
            // create widget
            QTextEdit *text = new QTextEdit();
            tab->addTab(tab_sub,"Test subtab");

            Yes, this is "confusing" - I read it as "adding tab / page (with tile "Text Edit ') and widget.

            BTW the QTextEdit takes full space on the page.

            So far no "layout" needed - unless I add more widgets to new page .. Right ?

            tab_sub->addTab(text, tr("Text Edit"));
            text->append("Append text ");
            

            Addendum
            I have made some changes and realized I do not need the "sub tab " .l
            Not sure how to change`this code

               // create widget
                QTextEdit *text = new QTextEdit();
                // create new tab
                QTabWidget *tab = new QTabWidget();
                text->setText("The quick brown fox jumps over lazy dog");
            
            //    tab->addTab(text, tr("Text Edit"));
            //    text->append("Append text ");
            
            // add a new page to existing tab 
                ui->tabWidget->addTab(tab, "New tab test");
                // add widget to main tab
            
                // create subtab
             //    QTabWidget *tab_sub = new QTabWidget();
            
             //   tab->addTab(tab_sub,"Test subtab");
                // created unwnated sub tab 
                tab->addTab(text, tr("Text Edit"));
                text->append("Append text ");
            //#endif
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              tr is used for text translation. It's an old habit of mine to write my code so that translation can be implemented at anytime.

              As for your use case, just nuke your "tab" object and add text directly to your ui QTabWidget:

              ui->tabWidget->addTab(text, tr("Text Edit"));
              

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

              1 Reply Last reply
              2

              • Login

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