QSyntaxHighlighter not working for form created QTextEdit - c++

As the title suggests, highlighting doesn't seem to work with form created QTextEdit.
My QSyntaxHighlighter derrivate class is the one from Qt docs and my code (the one that doesn't work):
ui->setupUi(this);
HtmlHighlighter hl(ui->textEdit->document());
but if I do this it works fine:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
QTextEdit editor;
HighLighter highlighter(editor.document());
editor.show();
return app.exec();
}
Is there any way to get it to work with the form generated one?

Your highlighter is going out of scope at the end of the constructor. Put it on the heap and make it a member variable, and it should work.
class MainWindow
{
//...
private:
HtmlHighlighter * h1;
}
Then in your cpp file:
ui->setupUi(this);
hl = new HtmlHighlighter(ui->textEdit->document());
Hope that helps.

Related

Custom Qt controls in C++

I'm trying to use a custom control in Qt, but don't know how.
My current main code:
int main(int argc, char *argv[]) {
QApplication application(argc, argv);
MainWindow window;
window.setWindowState(Qt::WindowMaximized);
window.show();
return application.exec();
}
MainWindow ui has a QTextEdit and a PushButton. Now, I want to use another control that inherits from QTextEdit.
I've added a CodeEditor class that is taken from the documentation, but it doesn't appear in Qt Creator designer.

How to remove the horizontal line in a QWizard with a stylesheet?

I am working on stylesheet of a QWizard and I would like to remove the horizontal line just above the push button.
I've already posted a minimal example here, the question was solved by scopchanov from the minimal example, but I have some lines of code in my project that avoids the solution to work, so I post another question here.
Here is my code (the complete buildable example can be downloaded from the gist here):
licensewizard.h
#include <QWizard>
class LicenseWizard : public QWizard {
Q_OBJECT
public:
LicenseWizard(QWidget *parent = 0);
};
licensewizard.cpp
#include <QApplication>
#include <QtWidgets>
#include "licensewizard.h"
LicenseWizard::LicenseWizard(QWidget *parent) : QWizard(parent) {
setWizardStyle(ModernStyle);
// solution from #scopchanov https://stackoverflow.com/a/52541248/8570451
QPalette p(palette());
p.setColor(QPalette::Mid, p.color(QPalette::Base));
setPalette(p);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// this line breaks #scopchanov solution.
// replace QLabel by QPushButton, or anything else... still broken.
qApp->setStyleSheet("QLabel { color:black; }");
LicenseWizard wizard;
wizard.show();
return app.exec();
}
As scopchanov said, I used the QPalette trick. But I have a big style sheet defined on the qApp and this is the cause of my problem. Using a very small style give the same problem.
The step to reproduce is to add this line after the declaration of the QApplication:
qApp->setStyleSheet("QLabel { color:black; }");
I hope someone could help me.
To fix this, set the palette of the whole application, instead of just the LicenseWizard class, like this:
LicenseWizard::LicenseWizard(QWidget *parent) : QWizard(parent) {
setWizardStyle(ModernStyle);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QPalette p(qApp->palette());
p.setColor(QPalette::Mid, p.color(QPalette::Base));
qApp->setPalette(p);
qApp->setStyleSheet("QLabel { color:black; }");
LicenseWizard wizard;
wizard.show();
return app.exec();
}
Note: As mentioned in my answer to the linked question, if this color role is used by any other item, its color would be affected as well.

can't group Push Buttons

i'm trying to group multi (Push Buttons) into an exclusive group, but i don't know how,
when i tried this example [Link] on its own it worked,
but when i try it on my project it failed
NOTE: all my UI elements are graphically implemented not code implemented,
also i'm using (Qt Creator) only
i tried the following
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Test01 window;
QButtonGroup apple (&window);
apple.addButton(&PB01);
apple.addButton(&PB02);
apple.addButton(&PB03);
apple.setExclusive(true);
window.show();
return app.exec();
}
but it didn't work, not the following either
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Test01 window;
QButtonGroup apple (&window);
apple.addButton(&ui->PB01);
apple.addButton(&ui->PB02);
apple.addButton(&ui->PB03);
apple.setExclusive(true);
window.show();
return app.exec();
}
i tried on the cpp file, like the following
Test01::Test01(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Test01)
{
ui->setupUi(this);
QButtonGroup apple (&Test01);
apple.addButton(&ui->PB01);
apple.addButton(&ui->PB02);
apple.addButton(&ui->PB03);
apple.setExclusive(true);
}
but didn't work either
can you please provide the solution
also what's the deference between write this code in (main.cpp) & in (test01.cpp) file
to answer your question correctly, it would be nice to provide the ui-Code of your form.
Howevery, one reason could be, that you missed the checkable property of QPushButton.
To work as a button group every QPushButton must be marked as checkable either by code:
PB01->setCheckable(true);
or by Qt Designer.
Greetings, Thomas

Qt C++ connect QPushButton click

I have just started developing using QtGUI and I have checked out some tutorials and documentation, and by what I've read this should work.
#define CONNECT QObject::connect
void test();
QPushButton *lpTestBtn;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtProject w;
w.show();
lpTestBtn = w.window()->findChild<QPushButton*>("TestBtn");
CONNECT(lpTestBtn, SIGNAL(clicked()),qApp,SLOT(test()));
return a.exec();
}
void test()
{
lpTestBtn->setText("Hi");
}
But test() never gets called. Also, lpTestBtn is not null and is found correctly.
I have tried the following changes
CONNECT(lpTestBtn, SIGNAL(clicked()),qApp->activeWindow(),SLOT(test()));
CONNECT(lpTestBtn, SIGNAL(clicked()),w.window(),SLOT(test()));
I know I can just do QtProject::on_TestBtn_clicked(); but I'd like to get it working using CONNECT,SIGNAL and SLOT.
The test function in your code is not a slot, nor does it belong to the Q(Core)Application class as you seem to have used it.
The best would be is to move that one line in the test function to the connection with the lambda syntax. This will require C++11 support, but that is in quite common use these days.
You would use the new shiny signal-slot syntax. This would spare you some headache for runtime issues in the future because they would appear at compilation-time.
Therefore, you would write something like this:
main.cpp
#include <QMainWindow>
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
// Replace it with "QtProject".
QMainWindow w;
w.show();
QPushButton * lpTestBtn = w.window()->findChild<QPushButton*>("TestBtn");
QObject::connect(lpTestBtn, &QPushButton::clicked, [=]() {
lpTestBtn->setText("Hi");
});
return a.exec();
}
main.pro
TEMPLATE = app
TARGET = main
QT += widgets
CONFIG += c++11
SOURCES += main.cpp
Build and Run
qmake && make && ./main
Please also note that it is bad idea to create a global pointer for a QPushButton object. It not only leaks the memory, but you could have other issues, too.
1) QApplication doesn't have a test() slot in it. You need to make your own class, inheriting QApplication, and give that class a test() slot. What you've done is create a regular function, which won't help.
2) You didn't check the return value of connect, which means you didn't see that it was giving you an error, which would probably have given you some clues.
You can connect signals to the slots which are in classes derived from QObject so meta compiler can deduce slot calls. But your test() is global function. You have to put your slot function inside a class that derives from QObject and supports Q_OBJECT macro or define a lambda function ( with C++11 support) as Laszlo Papp showed.
Example:
// testwrapper.h
class TestWrapper : public QObject
{
Q_OBJECT
//...
public slots:
void test();
};
// main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtProject w;
w.show();
QPushButton * lpTestBtn = w.window()->findChild<QPushButton*>("TestBtn");
TestWrapper tw;
CONNECT( lpTestBtn, SIGNAL( clicked()),tw,SLOT( test()));
return a.exec();
}

Qt: QPushButton never shows up

I'm trying to learn Qt, with a fairly simple application:
#include <QtGui/QApplication>
#include <QPushButton>
#include <QDebug>
/* -- header begin {{{ */
class BareBase {
public:
BareBase();
};
class BareBones: public QApplication {
private:
BareBase* base;
public:
BareBones(int &argc, char **argv);
~BareBones();
};
/* -- header end }}} */
/* -- implementation begin {{{ */
BareBase::BareBase()
{
QPushButton hello("Hello world!");
hello.resize(100, 30);
hello.show();
}
BareBones::BareBones(int& argc, char** argv): QApplication(argc, argv)
{
qDebug() << "Creating new instance ... ";
base = new BareBase();
}
BareBones::~BareBones()
{
qDebug() << "Cleaning up ... ";
delete base;
}
/* -- implementation end }}} */
int main(int argc, char **argv)
{
//Q_INIT_RESOURCE(files);
BareBones app(argc, argv);
return app.exec();
}
Now, the problem is that the Button created in BareBase never shows up, and i'm puzzled why?
Your QPushButton is creating and display correctly but go out of scope when leaving BareBase constructor. Using a member variable or a pointer will solve your problem.
If you use a pointer, you should add your button to its parent. By this way the button will be automatically deleted when the parent will be deleted.
QPushButton might have shown up but not in the visible area of the widget. That's why, you should add all your widgets to the Layouts that are available in Qt to obtain the desired behaviour. Check out the docs here... It has examples also...
Also, basically you will be having a base QWidget or most probably QMainWindow on which all your controls will be present.. So, your QPushButton will be in the parent widget.. Your QApplication will contain your application specific information like setting the window, setting the font for your entire application kinda stuff..
Hope it helps..