如何根据用户输入动态更改任何项目的位置?
How do I change any item's position dynamically based on user input?
如何在其他函数中访问在 MainWindow 构造函数中声明和初始化的数据? ui->customPlot 上有什么方法可以帮到我吗?
我的 Qt MainWindow 构造函数中有以下代码:
QCPItemLine* vec1 = new QCPItemLine(ui->mainGraph);
vec1->start->setCoords(0, 0);
vec1->end->setCoords(4, 4);
我希望用户能够在 2x1 QTableWidget 中输入数字并更改箭头指向的位置。例如:如果用户在 table 中输入 2,1,箭头移动并从 0,0 指向 2,1。
据我所知:
void MainWindow::on_table1_cellChanged(int row, int column)
{
// how can I access vec1 from here, since it is declared only in the scope of the constructor?
}
(table1 是我的 QTableWidget 的名称。)
我尝试将 QCPItemLine* vec1 放入 mainwindow.h 但无法弄清楚如何解决 "No appropriate default constructor available" 错误,因为 QCPItemLine 构造函数依赖于仅在 [=34] 之后可用的数据=]->setupUI(this),在默认构造函数列表之后调用。
我也试过在 on_table1_cellChanged 函数中调用 QCPItemLine* vec1 = ui->customPlot->item(),但得到了这个错误:"cannot convert from 'QCPAbstractItem *' to 'QCPItemLine *'"。另外我知道这种方式是有风险的,因为我不能总是依赖 vec1 作为最近添加到我的 customPlot 的项目。
您可以使 vec1 成为 class 的(私有)成员,将其初始化为 nullptr 并在调用 setupUI 后设置它。
mainWindow.h
private:
QCPItemLine* m_vec1;
mainWindow.cpp
MainWindow::Mainwindow(QWidget* parent):
QMainWindow(parent),
m_vec1(nullptr)
{
ui->setupUi(this);
m_vec1 = new QCPItemLine(ui->mainGraph);
}
m_vec 也可以在您的 cell-changed-slot
中访问
如何在其他函数中访问在 MainWindow 构造函数中声明和初始化的数据? ui->customPlot 上有什么方法可以帮到我吗?
我的 Qt MainWindow 构造函数中有以下代码:
QCPItemLine* vec1 = new QCPItemLine(ui->mainGraph);
vec1->start->setCoords(0, 0);
vec1->end->setCoords(4, 4);
我希望用户能够在 2x1 QTableWidget 中输入数字并更改箭头指向的位置。例如:如果用户在 table 中输入 2,1,箭头移动并从 0,0 指向 2,1。
据我所知:
void MainWindow::on_table1_cellChanged(int row, int column)
{
// how can I access vec1 from here, since it is declared only in the scope of the constructor?
}
(table1 是我的 QTableWidget 的名称。)
我尝试将 QCPItemLine* vec1 放入 mainwindow.h 但无法弄清楚如何解决 "No appropriate default constructor available" 错误,因为 QCPItemLine 构造函数依赖于仅在 [=34] 之后可用的数据=]->setupUI(this),在默认构造函数列表之后调用。
我也试过在 on_table1_cellChanged 函数中调用 QCPItemLine* vec1 = ui->customPlot->item(),但得到了这个错误:"cannot convert from 'QCPAbstractItem *' to 'QCPItemLine *'"。另外我知道这种方式是有风险的,因为我不能总是依赖 vec1 作为最近添加到我的 customPlot 的项目。
您可以使 vec1 成为 class 的(私有)成员,将其初始化为 nullptr 并在调用 setupUI 后设置它。
mainWindow.h
private:
QCPItemLine* m_vec1;
mainWindow.cpp
MainWindow::Mainwindow(QWidget* parent):
QMainWindow(parent),
m_vec1(nullptr)
{
ui->setupUi(this);
m_vec1 = new QCPItemLine(ui->mainGraph);
}
m_vec 也可以在您的 cell-changed-slot
中访问