我有一個QTabWidget
它包含一個QPlainTextEdit。我已經成功地將操作添加到QTabWidget中,以便無論何時打開新標籤頁,新標籤頁中都會添加一個新的QPlainTextEdit。看代碼。將事件/動作連接到QPlainTextEdit
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPlainTextEdit>
#include <QMessageBox>
#include <QAction>
#include <QTextCursor>
#include <iostream>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("Tilde");
current_tab = 1;
on_action_New_triggered();
ui->tabWidget->setTabsClosable(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_action_New_triggered()
{
QString newTab = "Tab " + QString::number(current_tab);
ui->tabWidget->addTab(new QPlainTextEdit, newTab);
ui->tabWidget->setCurrentIndex(current_tab - 1);
current_tab++;
editor = qobject_cast<QPlainTextEdit *>(ui->tabWidget->currentWidget());
editor->setFocus();
/*connect(editor->document(), SIGNAL(cursorPositionChanged(QTextCursor)),
this, SLOT(on_editor_cursorPositionChanged()));*/
}
void MainWindow::on_actionNew_document_triggered()
{
on_action_New_triggered();
}
void MainWindow::on_action_Exit_triggered()
{
QMessageBox msg;
msg.addButton(QMessageBox::Yes);
msg.addButton(QMessageBox::No);
msg.setText("Exit program?");
int selection = msg.exec();
if (selection == QMessageBox::Yes)
qApp->exit(0);
}
// highlight current line
void MainWindow::on_editor_cursorPositionChanged()
{
QTextEdit::ExtraSelection highlight;
highlight.cursor = editor->textCursor();
highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
highlight.format.setBackground(QColor(240, 246, 217));
QList<QTextEdit::ExtraSelection> extras;
extras << highlight;
editor->setExtraSelections(extras);
}
的註釋的代碼會產生編譯錯誤:
QMetaObject::connectSlotsByName: No matching signal for on_editor_cursorPositionChanged()
我已經添加在頭文件的功能。
頭文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit>
#include <QTextCursor>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_action_New_triggered();
void on_actionNew_document_triggered();
void on_action_Exit_triggered();
void on_editor_cursorPositionChanged();
private:
Ui::MainWindow *ui;
QPlainTextEdit *editor;
qint8 current_tab;
};
#endif // MAINWINDOW_H
是的,它是在頭文件中的專用插槽下定義的。 – raze 2012-04-22 18:00:04
是否註釋掉了「連接」引起錯誤的實際行或僅僅是引用? – jdi 2012-04-22 18:05:26
我還沒有用完整的代碼編輯帖子。我猜測評論的行會導致錯誤。我仍然可以運行應用程序。 – raze 2012-04-22 18:07:18