簡體   English   中英

如何根據用戶輸入動態更改任何物品的位置?

[英]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中輸入數字並更改箭頭指向的位置。 例如:如果用戶在表格中輸入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中,但無法解決如何解決“沒有合適的默認構造函數可用”錯誤,因為QCPItemLine構造函數依賴於僅在ui-> setupUI(this)之后可用的數據,在默認構造函數列表之后調用。

我還嘗試在on_table1_cellChanged函數中調用QCPItemLine * vec1 = ui-> customPlot-> item(),但收到此錯誤:“無法從'QCPAbstractItem *'轉換為'QCPItemLine *'”。 另外,我知道這種方式很冒險,因為我不能總是依靠vec1作為添加到我的customPlot中的最新項。

您可以將vec1設為該類的(私有)成員,將其初始化為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM