gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:09:14 25 4
gpt4 key购买 nike

我有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com