将事件/操作连接到QPlainTextEdit

时间:2012-04-22 17:49:07

标签: c++ qt

我有一个QTabWidget,其中包含一个QPlainTextEdit。我已设法向QTabWidget添加操作,以便每当新选项卡打开时,新选项卡中还会添加新的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:没有匹配的信号   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

1 个答案:

答案 0 :(得分:1)

你的SLOT签名是不是错了?

/*connect(editor->document(), SIGNAL(cursorPositionChanged(QTextCursor)),
            this, SLOT(on_editor_cursorPositionChanged()));*/

应该是吗?

connect(editor->document(), SIGNAL(cursorPositionChanged(QTextCursor)),
            this, SLOT(on_editor_cursorPositionChanged(QTextCursor)));

此外,您对该插槽使用的命名约定可能与您的手动连接存在冲突。 Qt可能会尝试通过匹配名称来使用您的SLOT上的connectSlotsByName机制:on_<member>_<signal>

在这种情况下,该SLOT on_editor_cursorPositionChanged()的当前签名将与QPlainTextEdit editor成员匹配。然后,您使用错误的签名手动将文档连接到它。您可能应该创建另一个名为docCursorPosChanged(QTextCursor)

的插槽