Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTabWidget example



  • Hello all,
    Is there a QTabWidget example? One that programmaticly creates the control and places an edit box, a check box a button or something in some of the tabs?

    I am able to create the tab control but I don’t see my edit boxes.

    TABCTRLConversion=new QTabWidget(this);
    //Position the control
    TABCTRLConversion->setGeometry(5, 450, 500, 500);

    //We have to create a widget for each tab
    WIDGETTemperature=new QWidget(NULL);

    tempString="Temperature";
    TABCTRLConversion->addTab(WIDGETTemperature, tempString);
    WIDGETWeight1=new QWidget(NULL);
    tempString="Weight";
    TABCTRLConversion->addTab(WIDGETWeight1, tempString);
    WIDGETWeight2=new QWidget(NULL);
    tempString="Weight2";
    TABCTRLConversion->addTab(WIDGETWeight2, tempString);
    connect(TABCTRLConversion, SIGNAL(currentChanged(sint)), this, SLOT(OnSelchangeTabConversion(sint)));

    xSPosition1=30;
    yPosition1=500;
    yOffset1=50;
    yOffset2=22;
    EDITBOXCelsiusSize=71;
    STATICTemperature1=new QLabel(WIDGETTemperature);
    //Position the control
    STATICTemperature1->setGeometry(xSPosition1, yPosition1-yOffset2, 171, 22);
    STATICTemperature1->setText(tr("Convert °F to °C and K"));
    EDITFahrenheit=new QLineEdit(WIDGETTemperature);
    //Position the control
    EDITFahrenheit->setGeometry(xSPosition1, yPosition1, EDITBOXCelsiusSize, 22);
    EDITFahrenheit->setMaxLength(50);
    connect(EDITFahrenheit, SIGNAL(textChanged(QString)), this, SLOT(OnChangeEditFahrenheit(QString)));



  • @stretchthebits
    I don't know whether your code does not work. One simply creates a QWidget for each "page", and put that into the QTabWidget via addTab(), as you seem to have done.

    Then, like any other QWidget, each of those page widgets should have a QLayout, and you add your sub-controls onto the page layouts as usual.



  • @JonB What if I don't want to use QLayout and I want to position the edit boxes, check boxes, myself.
    In fact, when I change tabs, my OnSelchangeTabConversion(sint index) is not getting called.


  • Lifetime Qt Champion

    @stretchthebits said in QTabWidget example:

    SIGNAL(currentChanged(sint))

    There is no such signal in QTabWidget - this is also printed during runtime on the console.
    And please use the new signal/slot syntax - then you can't hit such problems anymore.



  • @Christian-Ehrlicher
    Thanks, I changed that part to
    connect(TABCTRLConversion, &QTabWidget::currentChanged, this, &CChemicalADlg::OnSelchangeTabConversion);

    and it worked. One problem solved. One more to go.


  • Lifetime Qt Champion

    @stretchthebits Then please mark the topic as solved, thx.


Log in to reply