2013-11-27 46 views
-2

我很新手而且輸了! 我有一個.cpp文件在我的qt項目和我自己的widget.cpp至少有圖紙!現在我想從另一個.cpp文件中獲取數據,從一個名爲outputtext的類中獲取數據,它有一個方法add(name,value)和std string!Qt從C++方法獲取字符串和Widget中的DrawText?

知道我想在我的widget.cpp導入這個蜇!我有一個表格,我在上面放了一個按鈕'get string'-button! 知道我有

void Widget::on_pushButton_clicked(){ 
// how can i use Qpainter to deaw the text in my widget? 

} 

所以,這是我的widget類:

#include "widget.h" 
#include "ui_widget.h" 
#include "outputtext.h" 

Widget::Widget(QWidget *parent) : 
    QWidget(parent), 
    ui(new Ui::Widget) 
{ 
    ui->setupUi(this); 
} 

Widget::~Widget() 
{ 
    delete ui; 
} 



void Widget::on_pushButton_clicked() 
{ 

//int outputtext.add(name,value); 
// i want to say get the name and value from outputtext class and draw it in the widget! 



} 

和其他CPP文件(的outputText)的添加方法(名稱,淡水河谷)的字符串:

unsigned int OutPutText::add(std::string name , std::string value) 
{ 
    ..... 
} 

請幫忙!!我認爲這很容易,但我無法從按鈕中獲得畫家的作品!

+0

我是否正確地理解這一點:你有一類是通過塗料繪製文本()方法中的一類和你想在提取文本的一些其他類一個PushButton從第三課點擊?也許你可以在你的問題中添加一些相關的代碼。 –

+0

你的問題很難遵循。 – drescherjm

+0

沒有幫助?我只是想在按鈕類中實現繪圖,但它不是可行的..我應該在paintevent!怎麼樣! – user3041899

回答

0

問題解決: 爲了從on_pushButton_clicked中獲取小部件中的文本和繪圖,我必須使用QPixmap和QGraphicsscene,以便我爲Paiter創建一個場景並顯示小部件上的圖形。 Pixmap的大小和我的小部件的大小一樣,並使像素圖透明...然後我必須使用QGraphicsviewer讓場景顯示在小部件上,如下面的代碼!

感謝任何方式......

void Widget::on_pushButton_clicked() 
{ 


    QPixmap *pixmap = new QPixmap(this->size()); 
    pixmap->fill(Qt::transparent); 
    QGraphicsScene *scene = new QGraphicsScene(this); 
    scene->addPixmap(*pixmap); 
    QPainter painter(pixmap); 

    painter.begin(pixmap); 
    painter.drawPixmap(QPoint(0,0), *pixmap); 
    painter.drawText(x,y,"my string"); 

    painter.end(); 

    QGraphicsView *view = new QGraphicsView(scene, this); 
    scene->addPixmap(*pixmap); 
    view->setStyleSheet("background: transparent"); 
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
    view->show(); 
} 
相關問題