简体   繁体   中英

Writing c++ dll with Qt GUI, displaying fails in custom widget

I am writing a dll for an application that I cannot modify. This application is sending requests to my dll and I can run this application in single request mode or automatic where it works in some kind of loop sending requests and processing responses. I am not aware of how exactly this automatic mode works and I don't have access to source code to see the difference.

In my dll I have a custom MainWindow class inheriting QMainWindow that contains two objects: LogWindow inheriting QPlainTextEdit, and custom Widget inheriting QWidget. I would like to display some text and change some properties in my widget on request from main application. It all works well with single request. However, in automatic mode only the text is updated. I need some help to also make updates in widget. Now some implementation details:

In my dll code I have global variables for application and main window:

QApplication * app = nullptr; 
MainWindow * win = nullptr;

I create them on loading of the dll:

app = new QApplication(*argc, argv);
win = new MainWindow;
win->show();
app->processEvents();

My MainWindow looks like this:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this, &MainWindow::message_received, ui->log_window, &LogWindow::appendMessage);
    connect(this, &MainWindow::colors_received, ui->custom_widget, &CustomWidget::set_colors);
}

void MainWindow::append_log(std::string const & str) {
    emit message_received(QString::fromStdString(str));
}
void MainWindow::display_colors(Colors const & colors) {
    emit colors_received(colors);   
}

I call 'append_log()' and 'display_colors()' on request from application. And these are the slots being called:

void LogWindow::appendMessage(const QString & message) {
    this->appendPlainText(message);
}

And in Custom Widget:

void CustomWidget::set_colors(Colors const & colors) {
    setStyleSheet("background-color: red");
}

On request from dll I am calling both 'win->append_log()' and 'win->display_colors()'. As I said in automatic mode only text gets displayed, but no update to colors. I am guessing PlainTextEdit::appendPlainText() method must be calling some update, but I cannot figure out how to duplicate the behaviour onto my class. I tried 'update()' and 'repaint()' but nothing seems to work.

I guess noone had similar problem before, but I would appreciate some suggestions on what to try or where to look for.

Update

Turns out that CustomWidget::set_colors() method is not called when running in "automatic" mode. I assumed it is called, because I am using exactly the same method to call it as with LogWindow::appendMessage() .

Now the question is why it is not called. Some code details:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void append_log(std::string const &);
    void display_colors(Colors const &);
signals:
    void message_received(QString const &);
    void colors_received(Colors const &);

private:
    Ui::MainWindow *ui;
};

class LogWindow : public QPlainTextEdit
{
public:
    LogWindow();
    LogWindow(QWidget * parent);
public slots:
    void appendMessage(QString const &);
};

class CustomWidget : public QWidget
{
    Q_OBJECT

public:
    CustomWidget(QWidget *);
    CustomWidget(QWidget *, Colors);

public slots:
    void set_colors(Colors const &);

};

From generated setupUi() function:

centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
log_window = new LogWindow(centralWidget);
log_window->setObjectName(QStringLiteral("log_window"));
log_window->setGeometry(QRect(10, 10, 421, 561));
custom_widget = new CustomWidget(centralWidget);
custom_widget->setObjectName(QStringLiteral("custom_widget"));
custom_widget->setGeometry(QRect(439, 9, 381, 331));
MainWindow->setCentralWidget(centralWidget);

Probably I am missing something, but to me it looks like both methods are called exactly the same way. Unfortunately only one of them is working. Looking for suggestions what could be the issue.

The slot/signal mechanism was not working with custom class Colors , because it was not registered in Qt Meta Object system. Adding this line before signal connection solved the problem:

qRegisterMetaType<Colors>("Colors");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM