2013-09-28 38 views
1

另一個QLabel鼠標按下好所以我想要做的是創建一個新的QLabel添加到QList,並把它放在我點擊其他QLabel我點擊的地方。創建並指定一個QLabel後QT

因此,這裏是我的代碼:

class CustomLabel : public QLabel 
    { 
     Q_OBJECT 
    public: 
     CustomLabel(); 
     void mousePressEvent(QMouseEvent* event); 

    private: 
     QList<QLabel *> pointsL; 
     QList<QPoint *> points; 
    }; 


    void CustomLabel::mousePressEvent(QMouseEvent *event) 
    { 
     points << new QPoint(event->pos()); 
     pointsL << new QLabel(this); 
     pointsL.at(pointsL.size()-1)->setText("+"); 
     pointsL.at(pointsL.size()-1)->setGeometry(QRect(points.at(points.size()-1)->rx(),, points.at(points.size()-1)->ry(), 1, 1)); 
    } 

我也試過:

pointsL.at(pointsL.size()-1)->move(points.at(points.size()-1)->rx(), points.at(points.size()-1)->ry()); 

這:

void CustomLabel::mousePressEvent(QMouseEvent *event) 
    { 
     points << new QPoint(event->pos()); 
     pointsL << new QLabel(this); 
     pointsL.at(pointsL.size()-1)->setText("+"); 
     pointsL.at(pointsL.size()-1)->move(*points.at(points.size()-1)); 
     pointsL.at(pointsL.size()-1)->setTabOrder(pointsL.at(pointsL.size()-1), this); 
    } 

當我點擊自定義標籤沒有任何反應。構造函數是空的。

感謝您的任何答案。

+0

不要以爲QLabel可以作爲容器構件,即它不能有另一個QLabel像一個孩子,並顯示它。這可能是新標籤沒有出現的原因。我不確定我完全理解你的情況。你是否嘗試過一個QGridLayout?鼠標事件需要由父QWidget處理。 – user2672165

+0

我發現了該問題的替代方案,不過謝謝! – user2826636

回答

0

父窗口在屏幕上可見之後添加的新窗口小部件應該顯式顯示,除非它們在佈局中。

所以基本上你應該加上:

pointsL.back()−>show();