vnewfiledialog.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <QtWidgets>
  2. #include "vnewfiledialog.h"
  3. VNewFileDialog::VNewFileDialog(const QString &title, const QString &name, const QString &defaultName,
  4. const QString &description, const QString &defaultDescription,
  5. QWidget *parent)
  6. : QDialog(parent), title(title), name(name), defaultName(defaultName),
  7. description(description), defaultDescription(defaultDescription)
  8. {
  9. setupUI();
  10. connect(nameEdit, &QLineEdit::textChanged, this, &VNewFileDialog::enableOkButton);
  11. connect(okBtn, &QPushButton::clicked, this, &VNewFileDialog::accept);
  12. connect(cancelBtn, &QPushButton::clicked, this, &VNewFileDialog::reject);
  13. }
  14. void VNewFileDialog::setupUI()
  15. {
  16. nameLabel = new QLabel(name);
  17. nameEdit = new QLineEdit(defaultName);
  18. nameEdit->selectAll();
  19. nameLabel->setBuddy(nameEdit);
  20. descriptionLabel = new QLabel(description);
  21. descriptionEdit = new QLineEdit(defaultDescription);
  22. descriptionLabel->setBuddy(descriptionEdit);
  23. okBtn = new QPushButton(tr("&OK"));
  24. okBtn->setDefault(true);
  25. cancelBtn = new QPushButton(tr("&Cancel"));
  26. QVBoxLayout *topLayout = new QVBoxLayout();
  27. topLayout->addWidget(nameLabel);
  28. topLayout->addWidget(nameEdit);
  29. topLayout->addWidget(descriptionLabel);
  30. topLayout->addWidget(descriptionEdit);
  31. QHBoxLayout *btmLayout = new QHBoxLayout();
  32. btmLayout->addStretch();
  33. btmLayout->addWidget(okBtn);
  34. btmLayout->addWidget(cancelBtn);
  35. QVBoxLayout *mainLayout = new QVBoxLayout();
  36. mainLayout->addLayout(topLayout);
  37. mainLayout->addLayout(btmLayout);
  38. setLayout(mainLayout);
  39. setWindowTitle(title);
  40. }
  41. void VNewFileDialog::enableOkButton(const QString &editText)
  42. {
  43. okBtn->setEnabled(!editText.isEmpty());
  44. }
  45. QString VNewFileDialog::getNameInput() const
  46. {
  47. return nameEdit->text();
  48. }
  49. QString VNewFileDialog::getDescriptionInput() const
  50. {
  51. return descriptionEdit->text();
  52. }