且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在多个小部件上显示相同的QPushButton

更新时间:2023-11-14 17:08:10

攻击这个。我想做的是将外观与实际的按钮对象分开。下面是一个示例代码,需要扩展以实现更多功能。



file :: VirtualQPushButton.h



  #ifndef VIRTUALQPUSHBUTTON_H 
#define VIRTUALQPUSHBUTTON_H
#include< QPushButton>
//! \class将信号转发到基本按钮的类
class VirtualQPushButton:public QPushButton
{
Q_OBJECT
public:
VirtualQPushButton(QPushButton& basebutton,QWidget * parent = 0):
QPushButton(parent),
m_button(& basebutton)
{
QObject :: connect(this,SIGNAL(clicked forwardClick()));
//不要忘记转发其他信号
}

受保护的插槽:
void forwardClick()
{
if m_button)
{
m_button-> click();
}
}

private:
QPushButton * m_button;
};
#endif // VIRTUALQPUSHBUTTON_H

file :: main.cpp

  #include< QtGui / QApplication> 
#include< QHBoxLayout>
#includeVirtualQPushButton.h

int main(int argc,char * argv [])
{
QApplication a(argc,argv);
QWidget w;

QPushButton * basebutton = new QPushButton(& w);
basebutton-> hide();
QObject :: connect(basebutton,SIGNAL(clicked()),& w,SLOT(close()));

VirtualQPushButton * buton1 = new VirtualQPushButton(* basebutton);
buton1-> setText(Buton1);
VirtualQPushButton * buton2 = new VirtualQPushButton(* basebutton);
buton2-> setText(Buton2);

QHBoxLayout * lyt = new QHBoxLayout();
lyt-> addWidget(buton1);
lyt-> addWidget(buton2);
w.setLayout(lyt);
w.show();

return a.exec();
}

让我知道你的意见,感谢您的回应。



CV


I have two widgets (Real and Fake) and one of them has a QPushButton. Now I want the same button to be shown in the other widget. How do I do it?

I dont want to create a copy, I want the same QObject on to be shown another widget at the same time without changing the parent.

As an example, in the following I want "mybutton" to be shown in both the widgets at the same time;

  QWidget *widgetReal = new QWidget();
  QWidget *widgetFake = new QWidget();
  widgetReal->setWindowTitle("Real");
  widgetFake->setWindowTitle("Fake");
  widgetReal->show();
  widgetFake->show();

  QGridLayout *layoutReal = new QGridLayout();
  QGridLayout *layoutFake = new QGridLayout();

  QPushButton *mybutton = new QPushButton();

  layoutReal->addWidget(mybutton);
  widgetReal->setLayout(layoutReal);

  layoutFake->addWidget(mybutton); //this changes the parent and this is not what I want to do
  widgetFake->setLayout(layoutFake);

  mybutton->show();

The reason I need this is because I have a bunch of MDISubWindows and they contain some controls (buttons, checkboxes....etc). Now I want to select some of these controls from these widgets and create a single widget. The reason am doing this is because, I dont want to display all of my MDISubwindow when am using only single button in it Any suggestions are really helpful. Thank you.

-CV

Anyways, I designed a hack to this. What I am trying to do is separate the appearance from the actual button object. Here's a sample code which needs to be extended for more functionality

file:: VirtualQPushButton.h

#ifndef VIRTUALQPUSHBUTTON_H
#define VIRTUALQPUSHBUTTON_H
#include <QPushButton>
//! \class Class that forwards signals to the base button
class VirtualQPushButton : public QPushButton
{
  Q_OBJECT
public:
  VirtualQPushButton (QPushButton &basebutton, QWidget * parent = 0) :
    QPushButton (parent),
    m_button (&basebutton)
  {
    QObject::connect(this, SIGNAL(clicked()), this, SLOT(forwardClick()));
    //don't forget to forward other signals too
  }

protected slots:
  void forwardClick()
  {
    if (m_button)
    {
      m_button->click();
    }
  }

private:
  QPushButton *m_button;
};
#endif // VIRTUALQPUSHBUTTON_H

file:: main.cpp

#include <QtGui/QApplication>
#include <QHBoxLayout>
#include "VirtualQPushButton.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;

    QPushButton *basebutton = new QPushButton (&w);
    basebutton->hide();
    QObject::connect (basebutton, SIGNAL (clicked()), &w, SLOT (close()));

    VirtualQPushButton *buton1 = new VirtualQPushButton (*basebutton);
    buton1->setText("Buton1");
    VirtualQPushButton *buton2 = new VirtualQPushButton (*basebutton);
    buton2->setText("Buton2");

    QHBoxLayout *lyt = new QHBoxLayout();
    lyt->addWidget(buton1);
    lyt->addWidget(buton2);
    w.setLayout(lyt);
    w.show();

    return a.exec();
}

Let me know what you opinions, thanks for all your responses.

CV