QPushButton *minimumSizeButton(const std::string &text, QWidget *parent) { QPushButton *button = new QPushButton(text.c_str(), parent); QSize textSize = button->fontMetrics().size(Qt::TextShowMnemonic, button->text()); QStyleOptionButton opt; opt.initFrom(button); opt.rect.setSize(textSize); button->setMinimumSize(button->style()->sizeFromContents(QStyle::CT_PushButton, &opt, textSize, button)); button->setMaximumSize(button->style()->sizeFromContents(QStyle::CT_PushButton, &opt, textSize, button)); return button; }
//Construct the base GUI, and connect the signals void FileLineEdit::init(QString title, QFileDialog::FileMode mode) { //Layout QHBoxLayout *layout = new QHBoxLayout(this); layout->setSpacing(0); layout->setMargin(0); //Line edit _line = new QLineEdit(); layout->addWidget(_line); CONNECT(_line, SIGNAL(textChanged(QString)), this, SLOT(updateFileName(QString))); //Button QPushButton *button = new QPushButton(tr("Select...")); //Reduce the button size so that it's not higher than the line edit, and not much larger than its text button->setMaximumHeight(_line->size().height()-5); button->setMaximumWidth(button->fontMetrics().boundingRect(button->text()).width()+15); layout->addWidget(button); CONNECT(button, SIGNAL(clicked()), this, SLOT(clicked())); //Create the file dialog and set its mode _fileDialog = new QFileDialog(this,title,QDir::homePath()); _fileDialog->setFileMode(mode); }