Qt: フォームにファイル選択フィールドを追加する (QLineEdit と「参照」ボタン) 質問する

Qt: フォームにファイル選択フィールドを追加する (QLineEdit と「参照」ボタン) 質問する

QLineEditフォームに「参照」ボタンを表示する必要があります。ユーザーがボタンをクリックすると、QFileDialog開くなどの処理が行われます。

これはかなり一般的なことですが、そのための既成の解決策が見つかりません。Qt Designer に、 のようなウィジェットQFileSelectやそれに似たものがあることを期待していましたが、同様のものは見つかりませんでした。

手作業で実装する必要がありますか? または、これを行う正しい方法は何ですか?

ベストアンサー1

手作業で実装する必要がありますか? または、これを行う正しい方法は何ですか?

はい、よくあることだという点では同意しますが、残念ながらこれを自分で実装する必要があります。幸いなことに、次のようにすれば簡単に実装できます。

MyMainWindow::createUI()
{
    label = new QLabel("foo");
    button = new QPushButton("Browse");
    connect(button, SIGNAL(clicked()), SLOT(browse()));
    layout = new QHorizontalLayout();
    layout->addWidget(label);
    layout->addWidget(button);
    setLayout(layout);
}

void MyMainWindow::browse()
{
    QString directory = QFileDialog::getExistingDirectory(this,
                            tr("Find Files"), QDir::currentPath());

    if (!directory.isEmpty()) {
        if (directoryComboBox->findText(directory) == -1)
            directoryComboBox->addItem(directory);
        directoryComboBox->setCurrentIndex(directoryComboBox->findText(directory));
    }
}

おすすめ記事