How to get cursor shape? - c++

How can I get cursor shape when application minimized?
on hotkey I call:
qDebug() << "mouse (" << QCursor::pos().x() << "x" << QCursor::pos().y() << ")" << this->cursor().shape();
QCursor::pos() - is right, but cursor().shape() - always return "ArrowCursor" (even within the application).
How can I get real shape or cursor pixmap (Windows, MacOS)?

I don't think you can do this, at least not with Qt.
this->cursor() gives you a QCursor that was set on a certain QWidget using QWidget::setCursor. It's a simple accessor. It does not give you the current cursor being displayed (outside your program, window, not even outside the widget).

Related

Why is std::wcout not working after using FillConsoleOutputCharacter()?

I am currently working on a simple console application that is to become a text-based RPG game based on economy. I have an 80x20 (character cells) window that will work as my display.
I have been experimenting with the FillConsoleOutputCharacter() API function, which seems to work as desired for clearing the console.
Before I use the function, I have simple output using wcout to display certain characteristics, such as the screen buffer size and the window size; but after outputting with FillConsoleOutputCharacter(), I notice that wcout does not output as it should.
I have the API function nested in another function named clearConsole(), and have noticed that output using wcout works as it should after leaving the function, but not after the call to FillConsoleOutputCharacter() (within the function).
Here is the function:
void clearConsole(HANDLE screen)
{
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
DWORD dwConsoleSz;
if(!GetConsoleScreenBufferInfo(screen, &bufferInfo))
{
return;
}
dwConsoleSz = bufferInfo.dwSize.X * bufferInfo.dwSize.Y; /* This should be equivalent to the number of character cells in the buffer. */
/* Now we fill the entire screen with blanks. */
if(!FillConsoleOutputCharacter(
screen,
L' ',
dwConsoleSz,
cursorHome,
&cCharsWritten
));
{
return;
}
/* Then we get the current text attribute (maybe unnecessary?) */
if(!GetConsoleScreenBufferInfo(screen, &bufferInfo)) /* Perhaps the ScreenBuffer needs to be set again, see SetConsoleCursorPosition. */
{
return;
}
/* And set the buffer's attributes accordingly. */
if(!FillConsoleOutputAttribute(
screen,
bufferInfo.wAttributes,
dwConsoleSz,
cursorHome,
&cCharsWritten
))
{
return;
}
if(!SetConsoleCursorPosition(screen, bufferInfo.dwCursorPosition))
/*Research is needed regarding this function as it does not seem to move the cursor. */
{
wcout << L"SetConsoleCursorPosition failed. Error Code: " << GetLastError();
system("pause");
}
wcout << L"Screen Buffer Size: " << bufferInfo.dwSize.X << L',' << bufferInfo.dwSize.Y << L'\n' << L"Cursor Position: " <<
bufferInfo.dwCursorPosition.X << L',' << bufferInfo.dwCursorPosition.Y << L'\n' <<
L"Buffer Top X: " << bufferInfo.srWindow.Left << L' ' << L"Buffer Top Y: " << bufferInfo.srWindow.Top << L'\n' << L"Buffer Bottom X : " << bufferInfo.srWindow.Right << L' ' << L"Buffer Bottom Y: " << bufferInfo.srWindow.Bottom << L'\n';
wcout << L"hello from clearConsole" << L'\n';
/* wcout stops working from within this function after FillConsoleOutputCharacter is
called. But for some odd reason it begins working again after the function ends.
*/
Sleep(1000);
return;
}
What could be the cause for this?
I have experimented with moving the wcout lines around within the nesting function, and have seen that it works before the call to the API function, but not after. A strange malfunction. Flushing wcout (or the output stream) does not solve the issue, either.
Setting the cursor back to (0,0) does not seem to place the cursor back at the top left corner of the console, either. screen is a global variable set to STD_OUTPUT_HANDLE.
EDIT: Also, trying to set the cursor back to (0,0) using a global COORD object cursorHome in the call to SetConsoleCursorPosition() before writing to output does not seem to work, either.
EDIT: Setting the cursor's position after the call to clearConsole() works as well. What could be the cause of this strange behavior?
After multiple tests. It is caused by a very minor error - the ; after FillConsoleOutputCharacter().
/* Now we fill the entire screen with blanks. */
if(!FillConsoleOutputCharacter(
screen,
L' ',
dwConsoleSz,
cursorHome,
&cCharsWritten
)); // <-- HERE
{
return;
}
It cuts off the front and back connection and destroys the logic. It means that whatever value is returned by FillConsoleOutputCharacter() has nothing to do with the content in { }.
Just delete it.

Change x and y position of a QLineEdit

I would like to change the position of the lineEdit (or even a PushButton if it's not possible with the lineEdit) from my Qt application, according to the input given.
So let's say that I want the x position to be 150 pixels, then I would insert 150 into the lineEdit.
Is there any way to do this?
I've already tried this:
void DrawTest::on_lineEdit_returnPressed()
{
QString x = ui->lineEdit->text();
qDebug() << "x: " << x;
QString style = "QLineEdit {"
":" +ui->lineEdit->text()+ "px;"
"background-color: #FF00FF;"
"};";
qDebug() << "Style: " << style;
ui->lineEdit->setStyleSheet(style);
}
It depends on how the QLineEdit is initially positioned. Is it placed within a layout? If so, you won't be able to place it at an absolute position.
But if it does not belong to any layout, you can just use the move method:
ui->lineEdit->move(x, y);
Here's the docs.

std::cout re-printing everything currently in the console, each time it is used

For some reason, every time I use std::cout, the entire content (sort of, difficult to explain) of the console is re-printed, unless I << endl;. To provide some context, I am using glfw to back my Window class, which has higher level std::function callbacks. My compiler is MinGW 3.21, using what pieces of C++11 MinGW 3.21 actually implements. What is going on?
void Window::setTextCallback(std::function<void(char text)> callback) {
textCallback = callback;
auto onText = [](GLFWwindow* window, unsigned int text, int mods) {
Window* win = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
win->textCallback(static_cast<char>(text));
};
glfwSetCharModsCallback(window, onText);
}
And then in main.cpp...
Window w;
w.setTextCallback([](char text){
cout << text;
}
When the window is open, lets say I type "asdf". The output is "aasasdasdf". In slow motion, it goes: "a", "aas", "aasasd", aasasdasdf".
However, if I change main.cpp to:
Window w;
w.setTextCallback([](char text){
cout << text << endl;
}
The output is:
"a
s
d
f"
As expected.
No other threads are using cout and I know that because I don't have any other threads. This behavior does not happen elsewhere.
cout is a buffer. So each time you << to it, you're just adding text and it's holding on. endl is a way to flush the buffer. If you want to print only the last bit of text you push into it, you need to start with it empty. Try this post and then I'm sure your googling can finish any questions you might have about this.

Repeating loop until enter is pressed

For anybody else finding this question, this can be accomplished with the kbhit() function in the conio.h library. Just insert !kbhit() where I put SOMETHING and it will loop correctly, I am looking for a way to do this without a library, however.
I'm a beginner trying to create a simple animation in the console. The animation would have the word UP going up the right hand of the console and the word DOWN going down the right hand side. So far I have gotten the animation to complete one iteration of this, but I'm trying to make it so that it repeats and it looks like the texts wraps back to the top or bottom and does it again until the user presses the ENTER key.
My book (I'm self teaching from a textbook) makes it seem that its possible without any specific libraries except for iostream and windows.h but help that includes library functions is welcome too, it is a learning experience after all. Thanks a ton!
A little explanation of the code would be that I set the coordinates of the UP and DOWN starting positions and then move the cursor, delete the previous line it was on with a space and then increment two and put a new word. I would guess that I could use a second while loop to somehow check if the ENTER key has been pressed.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
COORD upPos = {20,28};
COORD downPos = {50, 0};
char endState;
while ( SOMETHING )
{
COORD upPos = {20,28};
COORD downPos = {50, 0};
while (upPos.Y >=0)
{
SetConsoleCursorPosition(screen,upPos);
cout << "UP" << endl;
upPos.Y++;
SetConsoleCursorPosition(screen,upPos);
cout << " " << endl;
upPos.Y -=2;
SetConsoleCursorPosition(screen,downPos);
cout << "DOWN" << endl;
downPos.Y--;
SetConsoleCursorPosition(screen,downPos);
cout << " " << endl;
downPos.Y+=2;
Sleep(100);
}
}
}
Your best bet is to create a custom "GetAsyncKeyState" function that will use #IFDEF for windows and linux to choose the appropriate GetAsyncKeyState() or equivalent. For ex
if (GetAsyncKeyState(VK_RETURN))
{
exit = true;
}

Find out deleted text of QPlainTextEdit

void QTextDocument::contentsChange(int position, int charsRemoved, int charsAdded) [signal]
This signal is emitted whenever the document's content changes; for example, when text is inserted or deleted, or when formatting is applied.
User can click cut/press delete/backspace or any other way and will remove text.
Problem is this signal emitted after text is removed. So I don't know which text was deleted.Now position and charsRemoved are of no use.
I want to find out deleted text of QPlainTextEdit. Is there any way to achieve this?
The trick with calling undo and redo suggested by Googie is nice, to make it even more efficient one can use a QTextCursor to extract the text instead of calling toPlainText:
void TextEdit::slotCheckRange(int pos, int removed, int added){
if(removed > 0){
undo();
QTextCursor c(textCursor());
c.setPosition(pos);
c.setPosition(pos + removed, QTextCursor::KeepAnchor);
qDebug() << "Removed: " << removed << " (" << c.selectedText() << ")";
redo();
}
if(added > 0){
QTextCursor c(textCursor());
c.setPosition(pos);
c.setPosition(pos + added, QTextCursor::KeepAnchor);
qDebug() << "Added: " << added << " (" << c.selectedText() << ")";
}
}
I see 2 possible solutions:
Store the contents of the document after every contents change, so in every next change you will have access to the previous contents and you will be able to extract the value using position and charsRemoved.
Pros: It's isolated mechanism and will not interfere with any other signals or slots.
Cons: It implies significant memory and CPU footprint (every text change causes full string copy).
(a better one in my opinion) In the slot function implementation use the undo() and redo() methods of the QPlainTextEdit to restore previous contents for the time of querying charsRemoved. Note, that calling undo() and redo() will not trigger the contentsChange() signal (I just tested it), so it's as simple as that.
Pros: Doesn't cause additional memory footprint. Not sure about the CPU footprint, but I think it's also better in that case.
Cons: This will only work with Undo/Redo mechanism enabled (which it is by default) and it also might affect any undo/redo code that you use or override (usually it's not the case).
To be clear, a sample code snipped for solution 2:
void MainWindow::textChanged(int pos, int rem, int add)
{
ui->plainTextEdit->undo();
qDebug() << ui->plainTextEdit->document()->toPlainText().mid(pos, rem); // <- text removed
ui->plainTextEdit->redo();
qDebug() << ui->plainTextEdit->document()->toPlainText().mid(pos, add); // <- text added
}