Qt World Summit: Register Today!

[SOLVED] Load content into tab at runtime on click using QTabWidget

  • This has also been posted here: http://stackoverflow.com/questions/30438887/load-content-into-tab-at-runtime-on-click-using-qtabwidget

    So I have this code in a file AnalysisDisplays.cpp

    void AnalysisDisplays::showAnalysisDisplays(QWidget *parent) {

    QMainWindow *mw = new QMainWindow(parent);
    mw->setWindowTitle("Equity Strategy Analysis");
    mw->setMinimumSize(750, 700);
    QTabWidget *tabw = new QTabWidget(mw);
    for (QListWidgetItem *wi: listItems) {
        if (wi->checkState()) {
            std::string eqType = wi->text().toStdString();
            DisplayAnalysis *dw = new DisplayAnalysis();
            displays[currentDisplayId] = dw;
            //Add this tab to tab widget
            tabw->addTab(dw, eqType.c_str());
    //Show the main window

    The issue is that we may create many tabs which is super slow. The commented out line is what I want to run at runtime. The issue is I am currently making a QMainWindow and a QTabWidget and forgetting about it. I need to somehow hook an event to the QTabWidget so whenever a tab object (DisplayAnalysis) dw is clicked, it will call dw->displayAnalysis(parseCSV->getDataForEquityType(eqType)); where eqType is the title of the tab. This will result in the tab content being populated.

    There can be multiple windows open that call showAnalysisDisplays and they may contain different number of tabs. So each window has its own QMainWindow and QTabWidget but the parseCSV pointer remains the same.

    So how can I hook an event to each tab like this, taking into account that there may be multiple windows open which need to load content at runtime when a tab is clicked?

  • The QTabWidget has a currentChanged signal - my first guess would be to create a slot that connects to that? Inside the slot you should be able to do currentWidget()->displayAnalysis(parseCSV->getDataForEquityType(eqType));

  • @Jakob Thanks this worked for me when combined with a solution posted on Stackoverflow! Thanks again!

Log in to reply