gpt4 book ai didi

c++ - 在两个类之间连接 QPushButton

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:02 25 4
gpt4 key购买 nike

我在 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));

}

在我的其他类(class)中,我构建了新的 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 对象。但我不明白如何解决这个问题。有人有想法吗?

最佳答案

问题是因为在 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

绘图.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()
{

}

关于c++ - 在两个类之间连接 QPushButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50962460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com