I see the confusion. Let me clarify exactly how the split-view Markdown editor should work and help debug what might be happening.
How the Split View Should Work
- Left Side (Source Editor): Shows the raw Markdown syntax exactly as typed
- Right Side (Preview): Shows the rendered result
When you type # Hello on the left, you should see:
- Left:
# Hello (the actual characters)
- Right: Hello (rendered as a large heading)
Debugging: Let's Verify Your Implementation
First, let's make sure you're using the split-view version correctly. Here's a complete test to verify everything is working:
Test 1: Create a New File
-
Open your Markpad application
-
In the left editor, type exactly this:
markdown
# This is a Header
This is a paragraph.
* Item 1
* Item 2
**Bold text** and *italic text*
-
As you type, the right preview should show:
- "This is a Header" as a large heading
- "This is a paragraph" as normal text
- Two bullet points
- "Bold text" in bold and "italic text" in italics
-
Save the file as test.md
-
Close and reopen the application, then open test.md again
Question: Does the left editor still show all the #, *, and ** characters?
If Syntax is Disappearing
If the Markdown syntax characters are disappearing from the left editor, here are the possible causes:
Cause 1: You might still be using the old single-editor version
Make sure your mainwindow.cpp has these key parts from Step 5:
cpp
void MainWindow::setupEditor()
{
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
// LEFT SIDE - Raw markdown source
sourceEditor = new QPlainTextEdit(this);
QFont font("Monospace");
font.setStyleHint(QFont::Monospace);
sourceEditor->setFont(font);
// RIGHT SIDE - Rendered preview
previewer = new QTextBrowser(this);
splitter->addWidget(sourceEditor);
splitter->addWidget(previewer);
setCentralWidget(splitter);
}
void MainWindow::updatePreview()
{
QString markdownText = sourceEditor->toPlainText();
previewer->setMarkdown(markdownText);
}
Cause 2: Saving/Loading Issue
Double-check your file operations use the correct methods:
cpp
void MainWindow::openFile()
{
// ... file dialog code ...
QTextStream in(&file);
sourceEditor->setPlainText(in.readAll()); // IMPORTANT: setPlainText, not setMarkdown
file.close();
}
void MainWindow::saveFile()
{
// ... file dialog code ...
QTextStream out(&file);
out << sourceEditor->toPlainText(); // IMPORTANT: toPlainText, not toMarkdown
file.close();
}
Complete Working Example
Here's a minimal complete example to test. Create a new Qt Widgets Application and replace the entire mainwindow.cpp with this:
cpp
#include "mainwindow.h"
#include <QSplitter>
#include <QPlainTextEdit>
#include <QTextBrowser>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Create splitter
QSplitter *splitter = new QSplitter(Qt::Horizontal, this);
// Create source editor (left)
sourceEditor = new QPlainTextEdit(this);
QFont font("Monospace");
font.setStyleHint(QFont::Monospace);
sourceEditor->setFont(font);
// Create previewer (right)
previewer = new QTextBrowser(this);
// Add to splitter
splitter->addWidget(sourceEditor);
splitter->addWidget(previewer);
setCentralWidget(splitter);
// Connect editor changes to preview updates
connect(sourceEditor, &QPlainTextEdit::textChanged,
this, &MainWindow::updatePreview);
// Create simple menu
QMenu *fileMenu = menuBar()->addMenu("File");
QAction *openAct = fileMenu->addAction("Open");
connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
QAction *saveAct = fileMenu->addAction("Save");
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
// Set initial content to test
sourceEditor->setPlainText("# Test Header\n\n* List item\n* Another item\n\n**Bold** and *italic*");
resize(1000, 600);
}
MainWindow::~MainWindow()
{
}
void MainWindow::updatePreview()
{
QString markdownText = sourceEditor->toPlainText();
previewer->setMarkdown(markdownText);
}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "",
"Markdown Files (*.md);;All Files (*)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Cannot open file");
return;
}
QTextStream in(&file);
sourceEditor->setPlainText(in.readAll());
file.close();
}
void MainWindow::saveFile()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save File", "",
"Markdown Files (*.md);;All Files (*)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Cannot save file");
return;
}
QTextStream out(&file);
out << sourceEditor->toPlainText();
file.close();
}
And update mainwindow.h:
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QPlainTextEdit;
class QTextBrowser;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void updatePreview();
void openFile();
void saveFile();
private:
QPlainTextEdit *sourceEditor;
QTextBrowser *previewer;
};
#endif // MAINWINDOW_H
This simplified version should clearly demonstrate that:
- The left side always shows raw Markdown syntax
- The right side shows the rendered result
- Saving preserves the exact text from the left side
Try this simplified version first to verify the concept is working, then we can debug your full implementation if needed.