Char is not large enough

Pages: 12
is there a way to either increase the size of char or declare a different data type s/t I can display this character in a widget? Right now the character is being truncated because char is only 8 bit and I am displaying a G instead of the char I am looking for.

 
  static_cast<char>(0x0147)
use a wide char wchar_t.
You are probably looking for wide characters.
https://en.wikipedia.org/wiki/Wide_character

You can also use UTF-8 and still store individual bytes as chars.
https://stackoverflow.com/a/23136957

You're trying to store Ň, right? Store it as two bytes and interpret it as UTF-8.
closed account (E0p9LyTq)
C++ has the wchar_t type, C++11 introduced char16_t and char32_t. All three types can store larger numbers used to represent characters.
@Manga

I tried wchar_t:



@Ganado

Yes I am trying to store Ň; "Store it as two bytes and interpret it as UTF-8" I am not sure how to do that.
Last edited on
QStrings have Unicode support. You shouldn't need to do anything special to store an Ñ. Post the code that causes the warning.
@helios

Last edited on
@vysero

Not exactly sure if this will work because I don't have Qt installed, but something like this:

QString str = QString::fromUtf8("Ň");
or try
QString str = QString::fromUtf8("\xc5\x88"); (less sure about this)

From: https://wiki.qt.io/Strings_and_encodings_in_Qt

As of QT5, apparently fromUtf8 is called internally, and no longer needs to be explicitly called.
______________________________

About your last post, we have no idea what createBtn is...
Last edited on
Do you mean to do something like this instead?
1
2
3
4
5
6
7
8
    QWidget *window = new QWidget;

    QPushButton *button1 = new QPushButton("Ň"); // or QPushButton(QString::fromUtf8("Ň"));
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(button1);

    window->setLayout(layout);
    window->show();

http://doc.qt.io/qt-5/qhboxlayout.html
Last edited on
Post the signature of createBtn(). Is it your function?
Sorry:
Last edited on
Your createBtn function is restricting you unnecessarily.

What is c2s? I assume that's converting a char to a QString? If you want to use unicode or utf8 strings, you can't do this. You have to call QString directly or make a better wrapper that can accept QStrings.
Last edited on
@Ganado
Last edited on
Or just change the char parameters to wchar_t. Then you can get rid of all those static_casts.
Yes, so, make something like this (not tested)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int KeyboardWidget::createBtn(QHBoxLayout* row, 
                              int          span, 
                              char         normal,
                              char         shift, 
                              char         caps, 
                              char         alt, 
                              QString      capAlt,
                              bool         autoRepeat)
{
  int id = m_allButtons.count();
  KeyboardWidgetButton* btn = new KeyboardWidgetButton(this, 
                                                       id,
                                                       c2s(normal), 
                                                       c2s(shift), 
                                                       c2s(caps), 
                                                       c2s(alt), 
                                                       capAlt);
  row->addWidget(btn, span);
  btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  btn->setAutoRepeat(autoRepeat);
  VERIFY(connect(btn,  SIGNAL(takeAction(int)),
                 this, SLOT(buttonClicked(int))));
  m_allButtons.append(btn);
  return id;
}


createBtn(row1, 2, '5', ')', '5', static_cast<char>(0xDF), QString("Ň"));
I assume the last argument is optional.

Edit: I think this will work, or just do what helios suggested.
Last edited on
By the way, you should avoid using non-ASCII characters in source code. C++ doesn't define how source files should be encoded, so when you write "Ñ" there's no telling how the editor is going to encode that (different editors will do different things), and how the compiler is going to decode it later (different compilers will also do different things).

Don't do it unless you know exactly what you're doing.
I see. So ("\xc5\x88") it is then.
@helios Truth be told this program was written by someone else and I am just trying to use it. I have very little experience with c++ and Qt GUI's. So I am attempting to learn by messing around with this stuff.

@Ganado I implemented the changes you suggested:

Last edited on
@helios I will try what you suggested:

const char* text,

changed to: const wchar_t* text, correct?
Last edited on
I don't see const char * being used anywhere in the code you've posted, so I don't know what you changed. Either way, my suggestion was to change the parameters of KeyboardWidget::createBtn().
Pages: 12