1
votes

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);
    mw->setCentralWidget(tabw);

    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());

            dw->setDisplayId(currentDisplayId);
            currentDisplayId++;

            //dw->displayAnalysis(parseCSV->getDataForEquityType(eqType));
        }
    }

    //Show the main window
    mw->show();
}

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?

1
Which part is the slow part? The parseCSV->getDataForEquityType(eqType) or the dw->displayAnalysis(..)? And do you really need to call it every time a tab is clicked, or just the first time it is clicked?thuga

1 Answers

2
votes

Use signals/slots to connect QTabWidget::currentChanged to a custom slot then get the tab and tab title using QTabWidget::tabText for example:

void AnalysisDisplays::currentChangedSlot(int index) {
    std::string eqType = tabw->tabText(index).toStdString();
    DisplayAnalysis *dw = qobject_cast<DisplayAnalysis*>(tabw->widget(index)));

    dw->displayAnalysis(parseCSV->getDataForEquityType(eqType));
}