c++ - 如何检查 ButtonGroup Id 并根据该 ButtonGroup id 更改另一个按钮

标签 c++ qt

我有 QButtonGroup,我在其中添加了 5 个 QPushButton 并为所有 QPushButton 设置了 id。现在,当 id 为 2 时,我想将 id 1 和 3 的 setSize 设置为 (100,100)。

QButtonGroup *button = new QButtonGroup;
button.addButton(button1,1);
button.addButton(button2,2);
..
..
button.addButton(button5,5);

现在我想当 button2 获得焦点时,我想将其大小设置为 (150,150),将 button1button3 的大小设置为 (100,100 ).

最佳答案

我明白了,所以这是你需要做的: 在我的 Dlg.h 中

  class MyPushButton : public QPushButton
  {
    public:

    MyPushButton(QString ButtonName, QWidget *parent);
    void focusInEvent(QFocusEvent* event);
    void focusOutEvent(QFocusEvent* event);
  };

在我的 Dlg.cpp 中:

   MyPushButton::MyPushButton(QString ButtonName, QWidget *parent)
   :QPushButton(ButtonName,parent)
   {
   }

   void MyPushButton::focusInEvent(QFocusEvent* event)
   {
     this->setMinimumHeight(150);
     this->setMinimumWidth(150);
   }

   void MyPushButton::focusOutEvent(QFocusEvent* event)
   {
     this->setMinimumHeight(100);
     this->setMinimumWidth(100);
   }

您不需要 QButtonGroup。现在您需要做的就是使用“MyPushButton”类并将按钮的默认高度和宽度设置为 100 *100。如果您有任何疑问,请告诉我。

MyMainWindow.cpp ,它的构造函数:

  MyMainWindow::MyMainWindow(QWidget *parent, Qt::WFlags flags)
  :QMainWindow(parent, flags)
 {
   ui.setupUi(this);
   this->setWindowTitle(QString::fromUtf8("MainWindow"));
   this->resize(250, 250);

   QWidget *centralWidget = new QWidget(this);

//Create QPushButtons
button1 = new MyPushButton("Button 1" , centralWidget);
button1->setMinimumHeight(100);
button1->setMinimumWidth(100);
button2 = new MyPushButton("Button 2" , centralWidget);
button2->setMinimumHeight(100);
button2->setMinimumWidth(100);
button3 = new MyPushButton("Button 3" , centralWidget);
button3->setMinimumHeight(100);
button3->setMinimumWidth(100);
button4 = new MyPushButton("Button 4" , centralWidget);
button4->setMinimumHeight(100);
button4->setMinimumWidth(100);

QHBoxLayout* layout = new QHBoxLayout(centralWidget);
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
layout->setSizeConstraint(QLayout::SetNoConstraint);
this->setCentralWidget(centralWidget);
    }

在 MyMainWindow.h 中

   class MyMainWindow: public QMainWindow
  {
 Q_OBJECT

   public:
 MyMainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);
 ~MyMainWindow();
public slots:
    void FileNew(int i);
    void keyReleaseEvent(QKeyEvent *e);

   private:
 Ui::StClass ui;
 MyPushButton* button1;
 MyPushButton* button2;
 MyPushButton* button3;
 MyPushButton* button4;
  };

关于c++ - 如何检查 ButtonGroup Id 并根据该 ButtonGroup id 更改另一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374785/

相关文章:

python - Phonon 可以播放 FLAC 文件吗?

c++ - 从数组中查找平均值

c++ - C++ 中的继承 : Error C2011

c++ - float 比较技巧 : inline assembly

qt - Qt:如何在GTK中创 build 置窗口

c++ - 通过 QDataStream 从客户端发送到服务器的字节不匹配

Qt unicode 和元音变音

c++ - 我们如何使用 OpencV StereoSGBM (C++) 显示单个视差图?

c++ - 语法帮助 : Row with all zeros in an else-if Statement

c++ - pnacl-ld : Invalid archive member header magic string