Qt:点击PushButton

时间:2017-01-19 08:08:54

标签: c++ qt

我有一个名为Cipher的基类和许多子类,如Xor,Caesar,Affine等

我有一个按钮列表和earch按钮,用于在单击按钮时实例化不同的对象

对于button1我想实例化child1 对于button2,我想实例化child2 等

    std::vector<std::string> ciphers = {"Xor", "Caesar", "Affine", "AveMaria", "Autoclave", "Vigenere"};

for (std::vector<std::string>::iterator it = ciphers.begin(); it != ciphers.end(); it++)
    associative_buttons.push_back(std::make_pair(*it, new QPushButton (QString::fromStdString(*it), this)));

int y = 15;
QFont font ("Courier");
for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
{
    (std::get<1>(*it))->setFont(font);
    (std::get<1>(*it))->setGeometry(40, 30, 160, 50);
    (std::get<1>(*it))->move(15, y);
    y += 50;

    QObject::connect(std::get<1>(*it), SIGNAL(clicked()), this, SLOT()); // my problem is here
}

有可能吗? 我有一个对象工厂,以防它可以提供帮助

编辑:

我收到一个错误:win.cpp:73:erreur:无法调用成员函数&#39; QObject * QObject :: sender()const&#39;没有对象          if(std :: get&lt; 1&gt;(* it)== QApplication :: sender())

这是正常的吗? 我的QApplication没有在那里宣布                                                       ^

#include "win.h"

Win::Win() : QWidget()
{
    c = nullptr;

    std::vector<std::string> ciphers = {"Xor", "Caesar", "Affine", "AveMaria", "Autoclave", "Vigenere"};

    for (std::vector<std::string>::iterator it = ciphers.begin(); it != ciphers.end(); it++)
        associative_buttons.push_back(std::make_pair(*it, new QPushButton (QString::fromStdString(*it), this)));

    int y = 15;
    QFont font ("Courier");
    for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
    {
        (std::get<1>(*it))->setFont(font);
        (std::get<1>(*it))->setGeometry(40, 30, 160, 50);
        (std::get<1>(*it))->move(15, y);
        y += 50;

        QObject::connect(std::get<1>(*it), SIGNAL(clicked()), qApp, SLOT(handleButtons()));
    }
}

Win::~Win()
{
    for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
        it = associative_buttons.erase(it);
}

constexpr unsigned int str2int(const char* str, int h = 0)
{
    return !str[h] ? 5381 : (str2int(str, h + 1) * 33) ^ str[h];
}

void Win::CreateInstance(std::string name)
{
  Cipher * instance = nullptr;

  switch (str2int(name.c_str()))
  {
    case str2int("Xor") :
      instance = new Xor();
      break;
    case str2int("Caesar") :
      instance = new Caesar();
      break;
    case str2int("Vigenere") :
      instance = new Vigenere();
      break;
    case str2int("Affine") :
      instance = new Affine();
      break;
    case str2int("AveMaria") :
      instance = new AveMaria();
      break;
    case str2int("Autoclave") :
      instance = new Autoclave();
      break;
    default:
      break;
  }

  c = instance;
}

void Win::handleButtons()
{
    for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
    {
        if (std::get<1>(*it) == QApplication::sender()) // error is here
            CreateInstance(std::get<0>(*it));
    }

    if (c != nullptr)
        c->run();
}

@ ab.o2c

编辑:

我最终使用QObject而不是QApplication修复它 这是代码,如果有人需要它

    #include "win.h"

Win::Win() : QWidget()
{
    c = nullptr;

    std::vector<std::string> ciphers = {"Xor", "Caesar", "Affine", "AveMaria", "Autoclave", "Vigenere"};

    for (std::vector<std::string>::iterator it = ciphers.begin(); it != ciphers.end(); it++)
        associative_buttons.push_back(std::make_pair(*it, new QPushButton (QString::fromStdString(*it), this)));

    int y = 15;
    QFont font ("Courier");
    for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
    {
        (std::get<1>(*it))->setFont(font);
        (std::get<1>(*it))->setGeometry(40, 30, 160, 50);
        (std::get<1>(*it))->move(15, y);
        y += 50;

        QObject::connect(std::get<1>(*it), SIGNAL(clicked()), this, SLOT(handleButtons()));
    }
}

Win::~Win()
{
    for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
        it = associative_buttons.erase(it);
}

constexpr unsigned int str2int(const char* str, int h = 0)
{
    return !str[h] ? 5381 : (str2int(str, h + 1) * 33) ^ str[h];
}

void Win::CreateInstance(std::string name)
{
  Cipher * instance = nullptr;

  switch (str2int(name.c_str()))
  {
    case str2int("Xor") :
      instance = new Xor();
      break;
    case str2int("Caesar") :
      instance = new Caesar();
      break;
    case str2int("Vigenere") :
      instance = new Vigenere();
      break;
    case str2int("Affine") :
      instance = new Affine();
      break;
    case str2int("AveMaria") :
      instance = new AveMaria();
      break;
    case str2int("Autoclave") :
      instance = new Autoclave();
      break;
    default:
      break;
  }

  c = instance;
}

void Win::handleButtons()
{
    for (std::list<std::pair<std::string, QPushButton*> >::iterator it = associative_buttons.begin(); it != associative_buttons.end(); it++)
    {
        if (std::get<1>(*it) == QObject::sender())
            CreateInstance(std::get<0>(*it));
    }

    if (c != nullptr)
        c->run();
}

1 个答案:

答案 0 :(得分:0)

我认为你可以用QSignalMapper做你想做的事。它的目的是映射来自一堆对象的信号并将它们引导到一个插槽,但添加了一个参数来枚举它们来自哪个对象。我认为这正是你想要做的。您只需创建映射器并为每个按钮分配每个按钮和唯一ID。它避免了为每个按钮创建唯一插槽的需要。

仅当原始信号没有任何参数时才有效,对于“点击”信号也是如此,所以对你来说应该没问题。