#include <QPlainTextEdit>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QGroupBox>
class FindDialog; // forward declaration
class ReplaceDialog : public FindDialog
{
Q_OBJECT
public:
explicit ReplaceDialog(QWidget *parent = nullptr, QPlainTextEdit *editor = nullptr);
private:
void setupReplaceUi();
QPushButton replaceBtn;
QPushButton replaceAllBtn;
QLabel replaceLabel;
QLineEdit replaceField;
private slots:
void handleReplaceOne();
void handleReplaceAll();
};
#include "ReplaceDialog.h"
ReplaceDialog::ReplaceDialog(QWidget *parent, QPlainTextEdit *editor)
: FindDialog(parent, editor)
{
setWindowTitle("Replace");
setupReplaceUi();
connect(&replaceBtn, &QPushButton::clicked, this, &ReplaceDialog::handleReplaceOne);
connect(&replaceAllBtn, &QPushButton::clicked, this, &ReplaceDialog::handleReplaceAll);
}
void ReplaceDialog::setupReplaceUi()
{
replaceBtn.setText("Replace");
replaceAllBtn.setText("Replace All");
replaceLabel.setText("Replace with:");
m_gLayout.removeWidget(&m_checkBox);
m_gLayout.removeWidget(&m_gBox);
m_gLayout.removeWidget(&m_cancleButton);
m_gLayout.addWidget(&replaceLabel, 1, 0);
m_gLayout.addWidget(&replaceField, 1, 1);
m_gLayout.addWidget(&replaceBtn, 1, 2);
m_gLayout.addWidget(&replaceAllBtn, 2, 2);
m_gLayout.addWidget(&m_checkBox, 3, 0);
m_gLayout.addWidget(&m_gBox, 3, 1);
m_gLayout.addWidget(&m_cancleButton, 3, 2);
setLayout(&m_gLayout);
}
void ReplaceDialog::handleReplaceOne()
{
if (!m_plainTextEdit || m_findLineEdit.text().isEmpty() || replaceField.text().isEmpty())
return;
QString searchTerm = m_findLineEdit.text();
QString replacement = replaceField.text();
QString currentSelection = m_plainTextEdit->textCursor().selectedText();
if (currentSelection == searchTerm)
m_plainTextEdit->insertPlainText(replacement);
onFindButtonClicked();
}
void ReplaceDialog::handleReplaceAll()
{
if (!m_plainTextEdit || m_findLineEdit.text().isEmpty() || replaceField.text().isEmpty())
return;
QString searchTerm = m_findLineEdit.text();
QString replacement = replaceField.text();
QString fullText = m_plainTextEdit->toPlainText();
Qt::CaseSensitivity sensitivity = m_checkBox.isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
fullText.replace(searchTerm, replacement, sensitivity);
m_plainTextEdit->clear();
m_plainTextEdit->setPlainText(fullText);
}
The replace dialog extends the existing find functionality by adding a replacement input field and two action buttons. setupReplaceUi removes and repositions widgets inherited from the base class to accommodate the new controls within the grid layout.
handleReplaceOne performs a single substitution. It checks if the currently selected text matches the search term; if so, it inserts the replacement. Afterwards it triggers the next search operation, allowing the user to step through matches.
handleReplaceAll reads the entire document content, calls QString::replace with the appropriate case‑sansitivity flag, and then updates the editor in one operation. This batch approach is more efficient than iterating individually for large texts.
When integrating the dialog in to a main window, enstantiate ReplaceDialog and invoke show() (or exec()) from the menu or toolbar action. Because the dialog is designed as a self‑contained component, it can be reused across different editor contexts without modification.