DEV Community

海前 王
海前 王

Posted on

current index


#include <QApplication>
#include <QComboBox>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QPushButton>
#include <QMessageBox>

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private slots:
    void showCurrentIndex();

private:
    QComboBox *comboBox;
};

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(centralWidget);

    comboBox = new QComboBox(this);
    comboBox->addItem("Item 1");
    comboBox->addItem("Item 2");
    comboBox->addItem("Item 3");

    QPushButton *button = new QPushButton("Show Current Index", this);
    connect(button, &QPushButton::clicked, this, &MainWindow::showCurrentIndex);

    layout->addWidget(comboBox);
    layout->addWidget(button);

    setCentralWidget(centralWidget);
}

void MainWindow::showCurrentIndex() {
    int index = comboBox->currentIndex();
    QString itemText = comboBox->currentText();
    QMessageBox::information(this, "Current Index", QString("Current Index: %1\nCurrent Item: %2").arg(index).arg(itemText));
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MainWindow window;
    window.show();

    return app.exec();
}

#include "main.moc"
Enter fullscreen mode Exit fullscreen mode

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay