I made a custom widget TabButton to group together some buttons I want to have change the ui based on the objectName of the button that was clicked.
header file:
class TabButton : public QPushButton
{
Q_OBJECT
private:
public:
TabButton(QWidget *parent = nullptr);
signals:
void updateTab(std::tuple<int,int> tabID);
public slots:
void tabButtonClicked();
};
cpp file:
TabButton::TabButton(QWidget *parent)
: QPushButton{parent}
{
connect(this, &TabButton::clicked, this, &TabButton::tabButtonClicked);
}
void TabButton::tabButtonClicked() {
std::string name = this->objectName().toStdString();
std::tuple<int,int> id = std::tuple<int, int>{std::stoi(name.substr(2, name.find("_")-2)), std::stoi(name.substr(name.find("_")+1))};
std::cout << "tabButtonClicked" << std::get<0>(id) << "," << std::get<1>(id) << std::endl;
qDebug() << "Button clicked. Emitting updateTab signal.";
emit updateTab(id);
}
The console prints “tabButtonClicked” when any of the promoted “TabButton” buttons are clicked, but in mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow) , modelPtr(new Model()), tabButton(new TabButton())
{
ui->setupUi(this);
connect(tabButton, &TabButton::updateTab, this, &MainWindow::handleTabUpdate);
qDebug() << "Connection established between TabButton and MainWindow.";
}
void MainWindow::handleTabUpdate(std::tuple<int,int> tabID) {
qDebug() << "handleTabUpdate called with tabId:" << std::get<0>(tabID) << "," << std::get<1>(tabID);
int row = std::get<0>(tabID);
int column = std::get<1>(tabID);
ui->sectionTabs->setCurrentIndex(row);
QTabWidget *nestedTab = ui->sectionTabs->findChild<QTabWidget *>(QString::fromStdString("s" + std::to_string(row) + "Widget"));
nestedTab->setCurrentIndex(column);
}
only “Connection established between TabButton and MainWindow.” is printed.
“handleTabUpdate called with tabID:…” is never printed, and the ui tabs never change.
Any ideas as to what’s wrong?
I tried messing with the header files and making sure everything looked correct, but no luck.
Logan Luker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.