QTextDocument will enter a new paragraph only after "Enter" is pressed 3 times - qt6

After filling QTextDocument with two paragraphs separated with empty line, to insert a new paragraph with empty line between them I need to press Enter button 3 times, instead of two expected. This happens with Qt 6.3.1. Do I do something wrong, or this is an issue of Qt?
#include <QTextEdit>
#include <QApplication>
#include <QTextDocument>
void fillTextDocument(QTextDocument *doc, const QString &text)
{
QTextCursor c(doc);
c.movePosition(QTextCursor::End);
QTextCharFormat fmt = c.charFormat();
QTextBlockFormat b = c.blockFormat();
fmt.setFontWeight(QFont::Normal);
fmt.setFontItalic(false);
fmt.setFontUnderline(false);
fmt.setAnchor(false);
fmt.setAnchorHref(QString());
fmt.setForeground(QBrush(Qt::black));
b.setAlignment(Qt::AlignLeft);
QFont f = fmt.font();
f.setPixelSize(15);
fmt.setFont(f);
c.setCharFormat(fmt);
c.setBlockFormat(b);
c.insertText(text);
c.movePosition(QTextCursor::End);
doc->clearUndoRedoStacks();
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTextEdit e;
fillTextDocument(e.document(), QStringLiteral("Hello\n\nHello"));
e.resize(600, 400);
e.show();
return QApplication::exec();
}
Guys, I consider this as a bug of Qt 6.3.1, regression in comparison with Qt 5.15. With Qt 5.15 the problem is not reproducible.

Related

How to fix "expected identifier or '(' in C compilation?

I am new to coding and I keep getting stuck in the first few lines of code and I cannot figure out why. This is what I have so far:
#include <stdio.h>
#include <cs50.h>
int main(void);
int n;
{
printf("Minute: ");
int n = get_int();
}
I am getting this message when I try to compile the code:
What did I do wrong?
You're trying to call the main function. You should only define it. It will be called when the program is executed (it is the "entry point").
To define it, remove the semicolon after
int main(void)
You can also remove that void keyword
Then move that line down, between
int n; and the { that comes after it
Additionally, you're declaring the n variable twice. After you fix the first error, the compiler will complain about this one. Remove one of the declarations then.
You should remove the semicolon after int main(void) and move the variable declaration for n within the braces. Here is the correct code below.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
printf("Minute: ");
int n = get_int();
}

Simple Cypher Program Not Working (CS50)

On week 2 of CS50 and I've hit a wall. My code is supposed to prompt a user for plaintext and then print a simple cypher on the next line. Problem is, my code keeps printing the exact input for the user rather than scrambling. My code is below.
Note: the error in my code is likely down in the for loop, inside the respective printf functions.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (int argc, string argv[]){
if (argc != 2){
printf("You must enter two arguments, the second being a single digit integer!\n");
return 1;
}
int key = atoi(argv[1]);
printf("What do you want to encrpyt?");
string s = get_string();
for(int i=0; i < strlen(s); i++){
if (isupper(s[i])==true){
printf("%c",((s[i] + key)));
}
if (islower(s[i])==true){
printf("%c",s[i] + key);
}
else {
printf("%c",s[i]);
}
}
}
Fixed it. The if statement syntax was wrong, so the program was skipping over the cypher text. I need to delete the "==true" out of the if statement.

fltk setting button's active label color

I am using fltk 1.3.2.
I set the button's label color with
_button->labelcolor(fl_rgb_color(162, 60, 62));
but when I press the button, the color is changed.
I couldn't find the function how set the active label color.
Does anyone know how to do that?
Edit:
I use Fl::background() and Fl::foreground() functions before creating the window. This make the problem.
Edit2:
This example shows the problem.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <iostream>
void HitMe(Fl_Widget* w)
{
std::cout << "Ouch" << std::endl;
}
int main(int argc, char ** argv)
{
Fl::background(0x60, 0x66, 0x60);
Fl_Window *window = new Fl_Window(320,130);
Fl_Button *b = new Fl_Button(10, 10, 130, 30, "A red label");
b->labelcolor(fl_rgb_color(162, 60, 20));
b->callback(HitMe);
window->end();
window->show(argc,argv);
return Fl::run();
}
When I commented out Fl::background() function everything is alright.
What you are seeing is the contrasting colour (see commented out code below). FLTK does this when the button is pressed. It gets the colour of the button, works out the contrasting colour based on the foreground and background colours. Have a look at the help for fl_contrast for more details.
Basically, if there is enough contrast, it will use the foreground colour otherwise it will find a contrasting colour for your background.
What can you do about it?
do nothing - that is how it is
choose a lighter background colour which will satisfy the contrast conditions
make your own button type with its own draw method
class KeepFGButton : public Fl_Button
{
public:
KeepFGButton(int x, int y, int w, int h, const char* s)
: Fl_Button(x, y, w, h, s)
{
}
void draw() {
if (type() == FL_HIDDEN_BUTTON) return;
Fl_Color col = value() ? selection_color() : color();
draw_box(value() ? (down_box() ? down_box() : fl_down(box())) : box(), col);
draw_backdrop();
// Remove the code that changes the contrast
//if (labeltype() == FL_NORMAL_LABEL && value()) {
// Fl_Color c = labelcolor();
// labelcolor(fl_contrast(c, col));
// draw_label();
// labelcolor(c);
//}
//else
draw_label();
if (Fl::focus() == this) draw_focus();
}
};
int main(int argc, char ** argv)
{
Fl::background(0x60, 0x66, 0x60);
Fl_Window *window = new Fl_Window(320, 130);
Fl_Button *b = new KeepFGButton(10, 10, 130, 30, "A red label");
...
Try the following and let me know if, when you run it, the label becomes white. If it doesn't then there is possibly something else that you are doing that is not quite right. If it does, I don't know what the problem is.
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <iostream>
void HitMe(Fl_Widget* w)
{
std::cout << "Ouch" << std::endl;
}
int main(int argc, char ** argv) {
Fl_Window *window = new Fl_Window(320,130);
Fl_Button *b = new Fl_Button(10, 10, 130, 30, "A red label");
b->labelcolor(fl_rgb_color(162, 60, 20));
b->callback(HitMe);
window->end();
window->show(argc,argv);
return Fl::run();
}
This is definitely too late to help the author, but in the event that this helps anyone else searching for an answer to this still relevant issue in FLTK, I'm going to offer my solution:
If you dig through the source code for FLTK 1.3.4-2 (current stable as of this post), there are a couple of built-in colormap indices which are referenced when the drawing of shadow boxes or frames are called: FL_DARK3 (for the boxes) and FL_DARK2 (for the scrollbar back color in Fl_Scroll). You can look at this FLTK documentation to see how to reset these colors to anything you wish at runtime. In particular, anticipating that this ugly red default mess will show up whenever there's a sufficiently dark background, it works well for me to just set these to a slightly lighter version of the overlayed box color:
Fl_Color boxColor = 0x88888800;
Fl_Color boxShadowColor = 0xaaaaaa00;
Fl::set_color(FL_DARK3, boxShadowColor);
Fl::set_color(FL_DARK2, boxShadowColor);
Now create your label as above and the display will be free of the red shadow. N.b. it is also possible to override the standard background2() behavior which resets FL_BACKGROUND_COLOR to the one produced by fl_contrast:
Fl::set_color(FL_BACKGROUND2_COLOR, yourBgColor);
The same trick works for other hard to reset colors like FL_INACTIVE_COLOR and FL_SELECTION_COLOR.
Hope this workaround helps.

Linux, C, Gtk. Close window after some time

Was trying to code. C, Linux. User enter some string and date. If it's today, then program should show a window with his string. Then this window should be closed by code, not user. And then it will pop up again after a short period of time. gtk_widget_destroy and gtk_widget_hide don't work.
`(aa:26429): Gtk-CRITICAL **: IA__gtk_widget_hide: assertion 'GTK_IS_WIDGET (widget)' failed`
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/* numbered markers placed below by msw for reference */
char str[50];
int main( int argc, char *argv[] )
{
printf("Enter your string\n>:");
fgets( str, 50, stdin );
char time_buf[10], date[10];
int a=strlen(time_buf);
int i=0;
time_t endwait;
time_t start = time(NULL);
time_t seconds = 30;
endwait=start+seconds;
printf("Enter date\n>:");
fgets(date, 10, stdin);
time_t now;
time(&now);
strftime(time_buf, 21, "%Y-%m-%d", gmtime(&now));
if (strncmp(time_buf,date,9) == 0) {
printf("TODAY!\n");
while (start < endwait) {
GtkWidget *label;
GtkWidget *window;
gtk_init( &argc, &argv );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW( window ),"ALARM");
label = gtk_label_new( str );
gtk_container_set_border_width(GTK_CONTAINER(window), 50);
gtk_container_add( GTK_CONTAINER( window ), label );
gtk_widget_show_all( window );
g_signal_connect(G_OBJECT(window),"destroy",
G_CALLBACK( gtk_main_quit ),NULL)
gtk_main(); // mark 1 ###
gtk_widget_destroy (label); // mark 2 ###
//gtk_widget_hide(window);
start = time(NULL);
sleep(10);
}
} else {
printf("NOT TODAY");
return 0;
}
}
GTK has an event loop, started with gtk_main. Actually, it is wrapping a glib event loop, to which you can add timeouts using g_timeout_add_full (or simply g_timeout_add). This is the good way to handle your issue (make some GTK signal handler register a Glib timeout which could e.g. call gtk_widget_hide or gtk_exit ...)
The big problem is that after you invoke gtk_main() at mark 1, your program does not get to run again until gtk_main exits. Prior to gtk_main returning, all widgets from the toplevel down will be destroyed. That's why I assume the error happened at mark 2 where you try to destroy a label that's already been destroyed.
It is still not clear to me why you'd want the behavior. Screen-wide popup notification behaves differently than gtk/glib/X11 programs because it is different. If I am running a program and a top-level window comes to the screen I should know what caused it and be able to get rid of it. It is more intuitive for the user if you scrap the popup, put a label below the entry¹ and change its text to "" when you want it to be invisible. And what #BasileStarynkevitch said.
¹What entry? The one that you should have to enter the string in. it would be an odd program that takes input from the console and displays output in a window.

atof truncates fractional parts after QApplication

I created a qt application with qt creator, the code is like,
#include "mainwindow.h"
#include <QDebug>
#include <QApplication>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double before = atof("3.1");
double x;
sscanf("3.1", "%lf", &x);
QApplication a(argc, argv);
double after = atof("3.1");
double y;
sscanf("3.1", "%lf", &y);
MainWindow w;
w.show();
qDebug() << before;
qDebug() << after;
qDebug() << x;
qDebug() << y;
return a.exec();
}
the output is
3.1
3
3.1
3
That means sscanf and atof truncates fractional parts after "QApplication a(argc, argv);". The problem only occurs in Qt5.3 under Linux Mint 17. I tested the same program in windows 8 and Mac OS 10.9, they don't have the same problem. Is it a bug in Linux Qt5.3 or it has something to do with linux c library?
The complete code can be accessed here
See QCoreApplication documentation:
On Unix/Linux Qt is configured to use the system locale settings by
default. This can cause a conflict when using POSIX functions, for
instance, when converting between data types such as floats and
strings, since the notation may differ between locales. To get around
this problem, call the POSIX function setlocale(LC_NUMERIC,"C") right
after initializing QApplication or QCoreApplication to reset the
locale that is used for number formatting to "C"-locale.
I can reproduce your problem and the following code fixes it for me:
QApplication a(argc, argv);
setlocale(LC_NUMERIC,"C");

Resources