I have a class that constructs a widget that I am able to dock into my main application. This class inherits QDockWidget
. This allows me to dock the widget if I so please. However, I would like for this widget to be docked by default instead of showing up as a separate floating window.
To give you an idea of how this class is laid out, here is the header for it. Consider that I want to keep the log
and showMessage
functions.
Logger.h
class Logger : public QDockWidget
{
Q_OBJECT
public:
explicit Logger(QWidget* parent = 0);
void log(QString message);
~Logger();
private:
QWidget* dockWidgetContents;
QGridLayout* gridLayout;
QTextEdit* LoggerEdit;
void showMessage(QString &message);
};
#endif // MESSAGES_H
In the .cpp file for my main application, I use loggerWidget = new Logger(this);
. This works, and when I open my application, the Logger widget pops up. I can then dock it into the sides of the main window on any side.
The problem I run into is getting this widget to be docked to the right side of the main window upon opening.
I've looked around for a solution and found that something similar to the following should work in the main window .cpp file. I just don't know how to implement it correctly.
LoggerWidget = new Logger(this);
this->setWidget(LoggerWidget);
addDockWidget(Qt::RightDockWidgetArea, LoggerWidget);
LoggerWidget->setFloating(false);
I think the issue is that since my Logger
class is inheriting QDockWidget
but isn't actually a QDockWidget
, so I can't just do an addDockWidget
in the main .cpp file.
How can I make this work while keeping the functionality that the class provides?
Assuming that the second pice of code:
LoggerWidget = new Logger(this);
this->setWidget(LoggerWidget);
addDockWidget(Qt::RightDockWidgetArea, LoggerWidget);
LoggerWidget->setFloating(false);
is within the constructor of a class that inherits from QMainWindow (otherwise you will not have functionalities such as addDockWidget
), you can expect a weird behaviour if you execute this code because you are adding the same widget (LoggerWidget
) to the central part of the window as well as to the dockable area (if it works you will see the exact same thing in both). Please find in the attached code a simple example of a QMainWindow with a central widget and a docked widget that inherits from QDockWidget:
Logger header
#ifndef LOGGER_H
#define LOGGER_H
#include <QDockWidget>
#include <QTextEdit>
class Logger : public QDockWidget
{
Q_OBJECT
public:
explicit Logger(QTextEdit* source, QWidget* parent = 0);
~Logger();
public slots:
void log(QString message);
private:
QTextEdit* LoggerEdit;
QTextEdit* texteditSource;
void showMessage(QString message);
};
#endif // LOGGER_H
Logger cpp
#include "logger.h"
Logger::Logger(QTextEdit* source, QWidget* parent):
QDockWidget(parent),texteditSource(source)
{
QDockWidget::setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
LoggerEdit = new QTextEdit();
LoggerEdit->setReadOnly(true);
QDockWidget::setWidget(LoggerEdit);
}
Logger::~Logger()
{
delete LoggerEdit;
}
void Logger::log(QString message)
{
showMessage(message);
}
void Logger::showMessage(QString message)
{
LoggerEdit->setText(message);
}
Main window widget header
#ifndef CUSTOMMAINWINDOW_H
#define CUSTOMMAINWINDOW_H
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextEdit>
#include "logger.h"
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
private slots:
void buttonClicked();
private:
QTextEdit* textEdit;
Logger* logger;
QPushButton* button;
};
#endif // CUSTOMMAINWINDOW_H
Main window widget cpp
#include "custommainwindow.h"
MainWindow::MainWindow(QWidget* parent):
QMainWindow(parent)
{
// Set window title
QMainWindow::setWindowTitle("Simple Example");
// Create central text
textEdit = new QTextEdit;// text edit to get text for the docked widget
// Create update button
button = new QPushButton("Update dockable widget");
// Create Logger inherited from QDockWidget
logger = new Logger(textEdit);
QMainWindow::addDockWidget(Qt::RightDockWidgetArea,logger);
// Set central widget
QMainWindow::setCentralWidget(new QWidget);
QMainWindow::centralWidget()->setLayout(new QVBoxLayout);
QMainWindow::centralWidget()->layout()->addWidget(textEdit);
QMainWindow::centralWidget()->layout()->addWidget(button);
// Connect for update docked wiget
QObject::connect(button,SIGNAL(clicked()),this,SLOT(buttonClicked()));
}
void MainWindow::buttonClicked()
{
logger->log(textEdit->toPlainText());
}