0
我是Qt新手,在Qt Creator 3.1.2(Ubuntu Linux),Qt 5.3.1中工作。Qt Creator中的僞裝部件
我的程序有一個帶有按鈕(pushButton
)的窗體,可以在按下時更改文本框的值(plainTextEdit
)。圖形模式下已添加pushButton
和plainTextEdit
。按鈕和插槽之間的連接(on_pushButton_clicked()
)也通過圖形界面設置。
問題是,程序在左上角產生一個僞造的plainTextEdit
,即輸出到達的位置,而「主」位置保持乾淨。因此,問題是,我怎樣才能避免它?一般來說,我應該如何連接圖形小部件和代碼中的對應部件?下面是我的程序:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QPlainTextEdit>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QPushButton *pushButton;
QPlainTextEdit *plainTextEdit;
};
#endif // MAINWINDOW_H
main.cpp中:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
plainTextEdit = new QPlainTextEdit(this);
// whenever I remove the previous line, I get SIGSEGV
setWindowTitle(tr("My test app..."));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
plainTextEdit->setPlainText("Some text here");
}