【问题标题】:Connect QPushButton between two classes在两个类之间连接 QPushButton
【发布时间】:2018-06-21 07:10:48
【问题描述】:

我在 Draw 类的构造函数中的 QGridLayout 中有一个 QListWidget。QGridLAyout 包含其他元素。

Draw::Draw(QWidget *parent):QDialog(parent){

gridlayout=new QGridLayout;
gridlayout->addLayout(hbox,0,0);
gridlayout->addLayout(hbox1,1,0);
gridlayout->addLayout(hbox2,2,0);
gridlayout->addWidget(listWidget,3,0);
gridlayout->addLayout(hbox4,4,0);
this->setLayout(gridlayout);

}

点击 QPushButton 时。我正在调用一个填充 QListWidget 的插槽。

//插槽

void Draw::fillListWidget(){
//item is QListWidgetItem
item=new QListWidgetItem;
item->setSizeHint(QSize(0,50));
listWidget->addItem(item);
//WidgetfillListWidget is an other class with parameters for the QListWidget
listWidget->setItemWidget(item,new WidgetfillListWidget(label1,path));

}

在我的其他课程中,我构建了新的 QWidget 来填充 QlistWidget。 QListWidget 包含一个标签、一个字符串和一个 QPushButton。

WidgetfillListWidget::WidgetfillListWidget(QString label, QString p){

instanceDraw=new Draw(this);
QString name = label;
QString path = p;

name1=new QLabel(name,this);
path1=new QLineEdit(path,this);
remove=new QPushButton("remove",this);

hboxlist=new QHBoxLayout;
hboxlist->addWidget(name1);
hboxlist->addWidget(path1);
hboxlist->addWidget(remove);

setLayout(hboxlist);
connect(remove,SIGNAL(clicked()),instanceDraw,SLOT(removeItem()));
}

在 Draw 类中,我有一个必须删除项目但它不起作用的插槽

void Draw::removeItem(){
listWidget->takeItem(listWidget->row(item));
}

删除该项目不起作用。我认为它来自连接中的 Draw 对象。但我不明白如何解决这个问题。有人有想法吗?

【问题讨论】:

  • takeItem 删除并返回给定行中的项目,然后您必须自己删除它delete listWidget->takeItem(listWidget->row(item));
  • 我有一个分段错误。我的 Draw 类实例可能有问题?
  • @Thomas1314 为什么要将 QString 添加为小部件? path1=new QString(path,this);hboxlist->addWidget(path1);
  • @eyllanesc 对不起。我错了。这是一个 QLineEdit ....
  • 但问题依旧...

标签: c++ qt c++11 qt5


【解决方案1】:

问题是因为在WidgetfillListWidget中创建的Draw和原来的Draw不同,它们是2个不同的对象。

在这种情况下,解决方案是在WidgetfillListWidget 中创建一个单击信号,该信号在按下QPushButton 时发出。

然后将该信号连接到removeItem() 方法。在其中我们必须获取项目,但这并不简单,一种方法是通过几何位置,如下所示:

widgetfilllistwidget.h

#ifndef WIDGETFILLLISTWIDGET_H
#define WIDGETFILLLISTWIDGET_H

#include <QWidget>

class QLabel;
class QPushButton;
class QHBoxLayout;
class QLineEdit;

class WidgetfillListWidget : public QWidget
{
    Q_OBJECT
public:
    explicit WidgetfillListWidget(const QString & name, const QString &path, QWidget *parent = nullptr);
signals:
    void clicked();
private:
    QLabel *lname;
    QLineEdit *lpath;
    QPushButton *premove;
    QHBoxLayout *hboxlist;
};

#endif // WIDGETFILLLISTWIDGET_H

widgetfilllistwidget.cpp

#include "widgetfilllistwidget.h"

#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

WidgetfillListWidget::WidgetfillListWidget(const QString &name, const QString & path, QWidget *parent) : QWidget(parent)
{
    lname=new QLabel(name);
    lpath=new QLineEdit(path);
    premove=new QPushButton("remove");

    hboxlist=new QHBoxLayout(this);
    hboxlist->addWidget(lname);
    hboxlist->addWidget(lpath);
    hboxlist->addWidget(premove);
    connect(premove, &QPushButton::clicked, this, &WidgetfillListWidget::clicked);
}

draw.h

#ifndef DRAW_H
#define DRAW_H

#include <QDialog>

class QGridLayout;
class QListWidget;
class QPushButton;

class Draw : public QDialog
{
    Q_OBJECT

public:
    Draw(QWidget *parent = 0);
    ~Draw();
private:
    void fillListWidget();
    void removeItem();

    QGridLayout *gridlayout;
    QListWidget *listWidget;
    QPushButton *button;
};

#endif // DRAW_H

draw.cpp

#include "draw.h"
#include "widgetfilllistwidget.h"

#include <QGridLayout>
#include <QListWidget>
#include <QPushButton>

Draw::Draw(QWidget *parent)
    : QDialog(parent)
{
    button = new QPushButton("Press Me");
    listWidget = new QListWidget;
    gridlayout=new QGridLayout(this);
    gridlayout->addWidget(listWidget, 0, 0);
    gridlayout->addWidget(button, 1, 0);
    connect(button, &QPushButton::clicked, this, &Draw::fillListWidget);
}

void Draw::fillListWidget()
{
    QListWidgetItem *item=new QListWidgetItem;
    item->setSizeHint(QSize(0,50));
    listWidget->addItem(item);
    //WidgetfillListWidget is an other class with parameters for the QListWidget
    QString label = "label1";
    QString path = "label2";
    WidgetfillListWidget *widget = new WidgetfillListWidget(label,path);
    listWidget->setItemWidget(item, widget);
    connect(widget, &WidgetfillListWidget::clicked, this, &Draw::removeItem);
}

void Draw::removeItem()
{
    WidgetfillListWidget *widget = qobject_cast<WidgetfillListWidget *>(sender());
    if(widget){
        QPoint gp = widget->mapToGlobal(QPoint());
        QPoint p = listWidget->viewport()->mapFromGlobal(gp);
        QListWidgetItem *item = listWidget->itemAt(p);
        item = listWidget->takeItem(listWidget->row(item));
        delete item;
    }
}

Draw::~Draw()
{

}

【讨论】:

  • 感谢您的回答。我不明白为什么要使用另一个 QPushButton “按钮”。因为我想从 premove 中删除
  • @Thomas1314 “按钮”是您提到的负责调用 fillListWidget 的按钮,您在问题中提到了它。
  • 对不起,我在问我的问题后看到按钮是什么......是的,我试过了,它有效!!!!非常感谢你!!!我认为要删除它刚刚占用的项目:listWidget->takeItem(listWidget->row(item))
  • @Thomas1314 如果您使用listWidget-&gt; takeItem (listWidget-&gt; row (item)) 并且您有很多项目,那么item 是什么意思? :)
猜你喜欢
  • 2012-06-01
  • 2013-08-06
  • 2014-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多