Here's the full code:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextBrowser>
#include <QPushButton>
#include <QMouseEvent>
#include <QTextCursor>
#include <QTextDocumentFragment>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= nullptr
);
~MainWindow();
private slots:
void handleInsert();
void handleSelect();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextBrowser>
#include <QPushButton>
#include <QMouseEvent>
#include <QTextCursor>
#include <QTextDocumentFragment>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void handleInsert();
void handleSelect();
private:
Ui::MainWindow *ui;
QPushButton *ButtonInsert;
QTextBrowser *ListWindow;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
ListWindow -> setText("This is a test");
connect(ListWindow, &QTextBrowser::cursorPositionChanged, this, &MainWindow::handleSelect);
connect(ButtonInsert, &QPushButton::released, this, &MainWindow::handleInsert);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handleInsert(){
//test code for learning
ButtonInsert->setText("Inserting...");
ButtonInsert->setText("Inserted");
}
void MainWindow::handleSelect(){
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ButtonInsert = new QPushButton("Insert", this);
ButtonInsert -> setGeometry(QRect(QPoint(440,90), QSize(100,30)));
ListWindow = new QTextBrowser(this);
ListWindow -> setGeometry(QRect(QPoint(50,50), QSize(300,500)));
ListWindow -> setText("This is a test");
connect(ListWindow, &QTextBrowser::cursorPositionChanged, this, &MainWindow::handleSelect);
connect(ButtonInsert, &QPushButton::released, this, &MainWindow::handleInsert);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::handleInsert(){
//test code for learning
ButtonInsert->setText("Inserting...");
ButtonInsert->setText("Inserted");
}
void MainWindow::handleSelect(){
ListWindow->moveCursor(QTextCursor::StartOfLine);
ListWindow->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
}
To copy to clipboard, switch view to plain text mode
Bookmarks