다음 솔루션을 제안합니다 (하위 클래스 버튼 클래스 없음). 실제로 QPushButtons
뿐만 아니라 모든 위젯의 동기화에 코드 벨로우즈를 사용할 수 있습니다.
SizeSynchronizer 클래스 - 클래스 사용의
/// Synchronizes the given widget's size with other's - one that the SizeSynchronizer installed on.
class SizeSynchronizer : public QObject
{
public:
SizeSynchronizer(QWidget *w)
:
m_widget(w)
{}
bool eventFilter(QObject *obj, QEvent *ev)
{
if (m_widget) {
if (ev->type() == QEvent::Resize) {
QResizeEvent *resizeEvent = static_cast<QResizeEvent *>(ev);
m_widget->resize(resizeEvent->size());
}
}
return QObject::eventFilter(obj, ev);
}
private:
QWidget *m_widget;
};
간단한 데모는 두 개의 버튼 동기화 :
int main(int argc, char *argv[])
{
[..]
// First button will be synchronized with the second one, i.e. when second
// resized, the first one will resize too.
QPushButton pb1("Button1");
QPushButton pb2("Button2");
// Create synchronizer and define the button that should be synchronized.
SizeSynchronizer sync(&pb1);
pb2.installEventFilter(&sync);
pb2.show();
pb1.show();
[..]
}
내가 더 "내장"솔루션이 있었다는 것을 희망
,하지만 결국 내가 그것을했다 당신이 제안한대로 잘 작동합니다. – Chris