Qt发送信号从动态组件

时间:2018-01-09 19:18:47

标签: c++ qt signals signals-slots

在尝试创建一个小型搜索GUI时,我希望每当用户键入TextEdit时,程序会搜索所有合理的结果并将其显示在QScollArea内的QGroupBox中。

我现在想补充一点,当我点击QGroupBox时,会打开一个新的QDialog并显示找到的结果详细信息。 (当它关闭时会自行删除 - 小细节窗口不是QGroupBox)

我设法做了所有事情,除了当我点击QGroupBox时,一个新的QDialog打开。

这是我的代码:
organizer.h

class Organizer : public QMainWindow
{
  Q_OBJECT

private:
    QGroupBox* generateSearchResultEnclosure(const std::string& item_name,const std::string& classer_name, const unsigned row, const unsigned column);
    std::vector<QGroupBox*> search_results;
    QGridLayout *scroll_layout;
    unsigned nRows;
public:
    explicit Organizer(ItemOrganizer* item_org,QWidget *parent = 0);
   ~Organizer();

private slots:
    void generateClickedWindowItemResult(const std::string& exact_item_name, QWidget* parent);
    void on_searchField_textChanged();
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void updateFormComponentAddAvailable();
    void updateFormAdd2ClsrAvailable();
    void updateFormInsrtClsrAvailable();
    void on_addClasser_PushButton_clicked();

private:
    Ui::Organizer *ui;
    ComponentAdd *form_cmp_add;
    AddToClasser *form_add2_clsr;
    InsertClasser *form_insrt_clsr;
    ItemOrganizer *item_organizer;

};

organizer.cpp
(关于QTextEdit文本更改):

void Organizer::on_searchField_textChanged()
{
   QWidget *wdg=ui->scrollArea->takeWidget();
   delete wdg;
   QWidget* blank_wdg=new QWidget();
   scroll_layout=new QGridLayout(blank_wdg);
   //widget->setLayout(layout);
   ui->scrollArea->setWidget(blank_wdg);
   ui->scrollArea->setWidgetResizable(true);

   if ((ui->searchField->toPlainText()).isEmpty())
      return;

   // Searching and generating a QGroupBox for each hit
   std::vector<SearchResult> res=item_organizer->search((ui->searchField->toPlainText()).toUtf8().constData());

   for (SearchResult &sr:res)
   {
       QGroupBox *win=generateSearchResultEnclosure(sr.cmp->name,sr.classer_name,sr.row,sr.column);
       scroll_layout->addWidget(win);
       win->show();
   }

   // Corecting QScrollArea widget size to fit all QGroupBox results
   unsigned row_count=0;
   if (scroll_layout->count()!=0)
       row_count=scroll_layout->rowCount();

   if(row_count>= nRows)
   {
         int h = 1.0*(ui->scrollArea->width() - scroll_layout->verticalSpacing()*(nRows+1.6))/nRows;
         int hCorrected = h*row_count + scroll_layout->verticalSpacing()*(row_count+2);
         ui->scrollArea->widget()->setFixedHeight(hCorrected);
   }
   ui->scrollArea->widget()->setFixedWidth(ui->scrollArea->viewport()->width());
}

organizer.cpp(生成QGroupBox的函数):

QGroupBox* Organizer::generateSearchResultEnclosure(const std::string& item_name,const std::string& classer_name, const unsigned row, const unsigned column)
{
QGroupBox* holder=new QGroupBox();
QVBoxLayout *layout_main=new QVBoxLayout;

QGroupBox *item_grp=new QGroupBox();
QGroupBox *clsr_grp=new QGroupBox();
QGroupBox *row_grp=new QGroupBox();
QGroupBox *column_grp=new QGroupBox();

QHBoxLayout *layout_item=new QHBoxLayout;
QHBoxLayout *layout_classer=new QHBoxLayout;
QHBoxLayout *layout_row=new QHBoxLayout;
QHBoxLayout *layout_column=new QHBoxLayout;

QLabel *item_label=new QLabel(tr("Item Name: "));
QLabel *clsr_label=new QLabel(tr("Classer Name: "));
QLabel *row_label=new QLabel(tr("Row: "));
QLabel *column_label=new QLabel(tr("Column: "));

QLineEdit *item_edit=new QLineEdit();
QLineEdit *classer_edit=new QLineEdit();
QLineEdit *row_edit=new QLineEdit();
QLineEdit *column_edit=new QLineEdit();

item_edit->setReadOnly(true);
classer_edit->setReadOnly(true);
row_edit->setReadOnly(true);
column_edit->setReadOnly(true);

item_edit->setText(item_name.c_str());
classer_edit->setText(classer_name.c_str());
row_edit->setText(QString::number(row));
column_edit->setText(QString::number(column));

layout_item->addWidget(item_label);
layout_item->addWidget(item_edit);

layout_classer->addWidget(clsr_label);
layout_classer->addWidget(classer_edit);

layout_row->addWidget(row_label);
layout_row->addWidget(row_edit);

layout_column->addWidget(column_label);
layout_column->addWidget(column_edit);

item_grp->setLayout(layout_item);
clsr_grp->setLayout(layout_classer);
row_grp->setLayout(layout_row);
column_grp->setLayout(layout_column);
layout_main->addWidget(item_grp);
layout_main->addWidget(clsr_grp);
layout_main->addWidget(row_grp);
layout_main->addWidget(column_grp);
layout_main->addStretch();
holder->setLayout(layout_main);
//holder->setMaximumHeight(ui->scrollArea->height()/2 - 10);
holder->setStyleSheet("QGroupBox {border: 2px solid gray; border-radius: 1px;} "
                      "QGroupBox::title {background-color: transparent; subcontrol-position: top left; padding:2 13px;}");

connect(holder, SIGNAL(clicked(bool)),this,SLOT(generateClickedWindowItemResult(item_name,holder)));
return holder;
}

现在我认为我无法将该SLOT连接到该信号(因为SIGNAL和SLOT需要具有相同的参数),即使这样的事情对我有用。

以下是我生成QDialog的部分,以防需要解决问题。 (虽然它只填写表格)

void Organizer::generateClickedWindowItemResult(const std::string& exact_item_name, QWidget *parent)
{
Component* cmp=item_organizer->getComponentByFullName(exact_item_name,true);
QDialog* cmp_window=new QDialog(parent);

QVBoxLayout *layout_main=new QVBoxLayout;

QGroupBox *item_name_grp=new QGroupBox();
QGroupBox *descr_grp=new QGroupBox();
QGroupBox *url_grp=new QGroupBox();
QGroupBox *tags_grp=new QGroupBox();
QGroupBox *quantity_grp=new QGroupBox();
QGroupBox *future_quantity_grp=new QGroupBox();

QHBoxLayout *layout_item_name=new QHBoxLayout;
QHBoxLayout *layout_descr=new QHBoxLayout;
QHBoxLayout *layout_url=new QHBoxLayout;
QHBoxLayout *layout_tags=new QHBoxLayout;
QHBoxLayout *layout_quantity=new QHBoxLayout;
QHBoxLayout *layout_future_quantity=new QHBoxLayout;

QLabel *item_name_label=new QLabel(tr("Item Name: "));
QLabel *descr_label=new QLabel(tr("Item Description: "));
QLabel *url_label=new QLabel(tr("Datasheet URL: "));
QLabel *tags_label=new QLabel(tr("Item Tags: "));
QLabel *quantity_label=new QLabel(tr("Item Current Quantity: "));
QLabel *future_quantity_label=new QLabel(tr("Item current+ordered quantity: "));

QLineEdit *item_name_edit=new QLineEdit();
QTextEdit *descr_edit=new QTextEdit();
QTextEdit *url_edit=new QTextEdit();
QTextEdit *tags_edit=new QTextEdit();
QLineEdit *quantity_edit=new QLineEdit();
QLineEdit *future_quantity_edit=new QLineEdit();

item_name_edit->setReadOnly(true);
descr_edit->setReadOnly(true);
url_edit->setReadOnly(true);
tags_edit->setReadOnly(true);
quantity_edit->setReadOnly(true);
future_quantity_edit->setReadOnly(true);

item_name_edit->setText(cmp->name.c_str());
descr_edit->setText(cmp->description.c_str());
url_edit->setText(cmp->url.c_str());

// TODO WRITE TAGS
//tags_edit->setText(cmp->tags);
quantity_edit->setText(QString::number(cmp->quantity));
future_quantity_edit->setText(QString::number(cmp->future_quantity));

layout_item_name->addWidget(item_name_label);
layout_item_name->addWidget(item_name_edit);

layout_descr->addWidget(descr_label);
layout_descr->addWidget(descr_edit);

layout_url->addWidget(url_label);
layout_url->addWidget(url_edit);

layout_tags->addWidget(tags_label);
layout_tags->addWidget(tags_edit);

layout_quantity->addWidget(quantity_label);
layout_quantity->addWidget(quantity_edit);

layout_future_quantity->addWidget(future_quantity_label);
layout_future_quantity->addWidget(future_quantity_edit);

item_name_grp->setLayout(layout_item_name);
descr_grp->setLayout(layout_descr);
url_grp->setLayout(layout_url);
tags_grp->setLayout(layout_tags);
quantity_grp->setLayout(layout_quantity);
future_quantity_grp->setLayout(layout_future_quantity);


layout_main->addWidget(item_name_grp);
layout_main->addWidget(descr_grp);
layout_main->addWidget(url_grp);
layout_main->addWidget(tags_grp);
layout_main->addWidget(quantity_grp);
layout_main->addWidget(future_quantity_grp);

layout_main->addStretch();
cmp_window->setLayout(layout_main);
cmp_window->setAttribute(Qt::WA_DeleteOnClose);
cmp_window->show();
}

如何连接来自QGroupBox的点击信号以生成QDialog并将参数传递给QDialog以及将该QGroupBox作为其父项?

0 个答案:

没有答案