如何解决QT中TableView中添加按钮时的错误



我成功地在TableView中的每一行中添加了一个按钮。然而,当我试图让他们连接到其他页面时,我被卡住了。

我想制作一个程序,当点击按钮时,会打开另一个页面。

以下代码是我的头文件:

//list of blog.h
#ifndef LISTOFBLOGS_H
#define LISTOFBLOGS_H
#include <QJsonArray>
#include <QDialog>
#include <QFont>
#include <QSignalMapper>
namespace Ui {
class listofblogs;
}
class listofblogs : public QDialog
{
Q_OBJECT
public:
explicit listofblogs(QWidget *parent = nullptr);
~listofblogs();
void setuserID(QString userID);
QString getuserID();
private slots:
void on_pushButton_clicked();
void doSomething(int row);
void map();
private:
Ui::listofblogs *ui;
QString UserID;
QSignalMapper *signalMapper;
};
#endif // LISTOFBLOGS_H

然后,这是C++源代码:

#include "listofblogs.h"
#include "ui_listofblogs.h"
#include "readandwritejson.h"
#include "blog.h"
void listofblogs::doSomething(int row){
qDebug()<<row;
}
listofblogs::listofblogs(QWidget *parent) :
QDialog(parent),
ui(new Ui::listofblogs)
{
ui->setupUi(this);
QFont font=ui->label_2->font();
font.setPointSize(16);
ui->label_2->setFont(font);
QTableWidget *tw = ui->tableWidget;
tw->clear();
tw->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
QStringList columns;
columns << "title" << "userID" << "time"<< "open content";
QJsonArray bloglist=readbloglist();
int row = bloglist.size();
int col = columns.size();
tw->setRowCount(row);
tw->setColumnCount(col);
for (int i = 0; i < col; ++i){
tw->setHorizontalHeaderItem(i, new QTableWidgetItem(columns.at(i)));
}
signalMapper = new QSignalMapper(this);
for( int i=0; i<row; i++ ) {
QJsonObject temp = bloglist.at(i).toObject();
for (int j = 0; j < col-1; j++){
auto item = tw->model()->index(i, j);
QString col_data = columns.at(j);
tw->model()->setData(item, QVariant::fromValue(temp.value("blogData").toObject().value(col_data).toString()));
}
//make new button for this row
auto item = tw->model()->index(i, columns.size()-1);
QPushButton *cartButton = new QPushButton("open");
cartButton->setMaximumWidth(50);
tw->setIndexWidget(item, cartButton);
connect(cartButton, SIGNAL(clicked(bool)), this, SLOT(map()));
//connect(cartButton, &QPushButton::clicked, signalMapper, &QSignalMapper::map);
signalMapper->setMapping(cartButton, i);
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(doSomething(int)));
for(int col = 0; col < columns.size(); col++) {
tw->resizeColumnToContents(col);
}
tw->horizontalHeader()->setStretchLastSection(true);
tw->setEditTriggers(QAbstractItemView::NoEditTriggers);
tw->scrollToBottom();
tw->verticalHeader()->hide();
tw->setSelectionMode(QAbstractItemView::NoSelection);
tw->resizeColumnsToContents();
}
listofblogs::~listofblogs()
{
delete ui;
}
void listofblogs::setuserID(QString userID){
UserID=userID;
}
QString listofblogs::getuserID(){
return UserID;
}
void listofblogs::on_pushButton_clicked()
{
blog *Blog;
Blog = new blog(this);
Blog->setuserID(UserID);
Blog->show();
}

我可以编译这段代码,但当我单击按钮时,错误发生了,因为我没有定义map()函数,但我如何定义它?(我根据这个问答制作了这个程序。(

另外,当我把这个部分从改过来时

connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(doSomething(int)));

至:

connect(signalMapper, &QSignalMapper::mappedString,
this, &listofblogs::doSomething(row));

我得到了这个错误,无法编译它:

listofblogs.cpp:51:9: error: no matching member function for call to 'connect'
qobject.h:197:36: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool)' to 'const char *' for 2nd argument
qobject.h:200:36: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool)' to 'const QMetaMethod' for 2nd argument
qobject.h:443:41: note: candidate function not viable: no known conversion from 'void (QAbstractButton::*)(bool)' to 'const char *' for 2nd argument
qobject.h:217:43: note: candidate template ignored: couldn't infer template argument 'Func2'
qobject.h:258:13: note: candidate template ignored: couldn't infer template argument 'Func2'
qobject.h:297:13: note: candidate template ignored: couldn't infer template argument 'Func2'
qobject.h:249:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided
qobject.h:289:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided

我建议您将较低级别的QTableViewQAbstractItemDelegate一起使用您可以将QAbstractItemDelegate子类化并在其中集成一个按钮,它们将按钮点击信号映射到一个插槽,所选页面将取决于模型的数据或代理的索引

相关内容

  • 没有找到相关文章

最新更新