QTextEdit에는 setMaxLength 함수가 없다


입력 글자수를 제한하기 위해서 다음과 같이 SLOT을 Connect한다


//member variable

QString tempMemo_ = {};



QTextEdit* memoEdit = dialog_->findChild<QTextEdit*>(toString(ControlID::editMemo));


if (memoEdit->toPlainText().length() > 256)//set maxlength 256

{


disconnect(memoEdit, SIGNAL(textChanged()), this, SLOT(slotValueChanged_memo())));


QTextCursor currentCursor = memoEdit->textCursor();


int pos = currentCursor.position();


memoEdit->setPlainText(tempMemo_);


currentCursor.setPosition(pos - 1, QTextCursor::MoveMode::MoveAnchor);


memoEdit->setTextCursor(currentCursor);


connect(memoEdit, SIGNAL(textChanged()), this, SLOT(slotValueChanged_memo())));


}


else

{

//keep text at last input

tempMemo_ = memoEdit->toPlainText();

}


by 무위자연 2018. 3. 12. 11:16

다음과 같은 요구사항이 있을 경우 상당히 구현하기 까다롭다


requirement : 

YYYY MM DD 로 제한없는 입력보다는 제한된 리스트를 제공하여 콤보박스 형태로 보여줌

단, 직접 입력도 가능하도록 처리

년: 1900~2018 까지 제공
월: 01~12 까지 제공
일: 01~31 까지 제공


이 때 쓸만한 것이 QCompleter


example

QLineEdit으로 text 입력을 받고 여기에 QCompleter를 붙이는 형태이다.

이 때 중요한 것은 model을 설정하는 것. 

1. 파일에서 모델을 읽어오기도 하고 

> 예제  completer->setModel(modelFromFile(":/resources/wordlist.txt")) :: (링크)

2. sql model을 설정하고 특정 컬럼의 데이터만 대상만 보이게 할수도 있다.

> 예제

QSqlQueryModel sample;

completer->setModel(sqmple);

completer->setCompletionColumn(specific column name);

completer->setCompletionMode(QCompleter::PopupCompletion);

3. 하기와 같이 간단하게 string model을 만들수도 있다.


QStringList stringList;

for (int i = 1; i <= selecteddate.daysInMonth(); i++)

{

stringList << QString().sprintf("%02d", i);

}

QStringListModel *dateModel = new QStringListModel(stringList);

QCompleter* dateCompleter = new QCompleter;

dateCompleter->setModel(dateModel);


dateEdit->setCompleter(dateCompleter); //dateEdit은 QLineEdit

by 무위자연 2018. 2. 22. 16:28

1 include header file

#include <QTcpSocket>

  #include <qabstractsocket.h>


2. declare a variable

QTcpSocket *tcpClient = Q_NULLPTR;


3 connect slot

connect(tcpClient, SIGNAL(connected()), this, SLOT(connectedToDM()));//check connected server

connect(tcpClient, SIGNAL(readyRead()), this, SLOT(ReceiveServerData())); //receive data from server

connect(tcpClient, SIGNAL(disconnected()), this, SLOT(disconnectedfromDM())); //receive signal when disconnected

connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));//// check socket error from server


4. try to connect to server

tcpClient->connectToHost(m_dm_ip, 8000);


5. try to disconnect from server

tcpClient->disconnectFromHost();


6. send message to server

qint64 result = tcpClient->write(data.constData());


7. receive data from server

void MainWindow::ReceiveServerData()

{   

QByteArray servermsg = tcpClient->readAll();    

    QString msg = QString::fromUtf8(servermsg.constData());

 }



8. control socket error

void MainWindow::displayError(QAbstractSocket::SocketError socketError)

{

    Log();

    if (socketError == QTcpSocket::RemoteHostClosedError)

        return;

by 무위자연 2017. 12. 19. 09:50


2 바이트를 읽어서

7 비트 / 4비트 / 5비트로 쪼개서 데이터를 parsing하는 예제


//2 바이트를 하나의 값으로 계산을 한다

 int value = (qint8(rcvPacket[1] & 0x0F) * 16) + qint8(rcvPacket[2] & 0x0F) + ((rcvPacket[4] & 0x0F)*16 + (rcvPacket[5] & 0x0F)) * 256;


//하위 비트를 제거하기 위해서 9만큼 쉬프트를 한다 그 다음에 7비트가 모두 1인 값(0x7F)와 AND 연산을 한다

 int year = (value >> 9) & 0x7F;


//하위 비트를 제거하기 위해서 5만큼 쉬프트를 한다 그 다음에 4비트가 모두 1인 값(0x0F)와 AND 연산을 한다

 int month = (value >> 5) & 0x0F;


//하위 5비트와 AND연산을 한다.

  int date = value & 0x1F;


by 무위자연 2017. 11. 29. 15:10

QMessageBox messageBox;

    QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);

    messageBox.setText( msg );

    messageBox.setStandardButtons(QMessageBox::Yes);

    messageBox.addButton(QMessageBox::No);

    messageBox.setDefaultButton(QMessageBox::No);

    QGridLayout* layout = (QGridLayout*)messageBox.layout();

    layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

    if( messageBox.exec() == QMessageBox::Yes)

      {

        //yes

      }

      else

      {

       //no

      }

-----------------------------------

버튼의 문구를 바꾸고 팝업 style을 주고 싶다면.

messageBox.setStyleSheet(

                " QLabel {"

                " min-width: 300em;"

                "font: bold 24px; "

                "}"

                "QPushButton {"

                "font: bold 24px; "

                "}"

                );

    //messageBox.setDefaultButton(QMessageBox::No);

    QAbstractButton *myYesButton = messageBox.addButton(tr("BUTTON_DELETE"), QMessageBox::YesRole);

    QAbstractButton *myNoButton = messageBox.addButton(tr("BUTTON_CANCEL"), QMessageBox::NoRole);

    QGridLayout* layout = (QGridLayout*)messageBox.layout();

    layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

    messageBox.exec();

    //if( messageBox.exec() == QMessageBox::Yes)

    if(messageBox.clickedButton() == myYesButton)


by 무위자연 2016. 11. 11. 15:08

QT 5.5로 샘플코드로 작성해서 해당 플랫폼이 지원하지 않음을 확인했습니다.

5.5 BLE feature에도 Windows 10에 대한 언급이 없으며 5.6에도 추가할 예정은 없는 것으로 보입니다.

해당 내용은 해당 버전에 대한 참고 링크입니다.

qt doesn;t support Windows 10 platform not yet

5.6 https://wiki.qt.io/New_Features_in_Qt_5.6 

5.5 https://wiki.qt.io/New_Features_in_Qt_5.5 


첨부된 파일은 간단하게 ble 테스트를 해 볼 수 있는 샘플코드입니다.

해당 파일은 QT 5.5에서 빌드가 가능합니다.

This sample is only for testing ble test at specific platform.

This is only running at qt library 5.5 not 5.4

blesupportcheck.zip




by 무위자연 2016. 1. 4. 15:04

ui에 QListWIdget 추가 된 상태.



ui->listWidget->addItem(str); // add str

ui->listWidget->setCurrentRow(ui->listWidget->currentRow() + 1); // move to current Row

ui->listWidget->item(ui->listWidget->currentRow())->setForeground(*(new QBrush(Qt::red))); // colored by RED


by 무위자연 2015. 12. 9. 17:06
 

    QByteArray s_byte;

    s_byte.append((char)0x7e);

    bool ok;

    int t = s_byte.toHex().toInt(&ok, 16);


    QByteArray tmp,tmp2;

    //tmp.append((char)0x00);

    //tmp.append((char)0x01);

    tmp.append((char)0x7e);

    tmp.append((char)0x0c);


    tmp2.append((char)0xff);

    tmp2.append((char)0xff);


    bool isok;

    int size = 0;

    //Log() <<  QString().sprintf("4 : %05f", tmp2.toDouble(&isok));

    size = tmp2.toHex().toInt(&isok, 16);

    Log() << size;

    size = tmp.toHex().toInt(&isok, 16);

    Log() << size;

    Log();


테스트는 1자리, 2자리까지만 해봄.

0~FFFF까지.


by 무위자연 2015. 11. 18. 14:06

시스템 날짜가 마지막 빌드 날짜 보다 훨씬(?)이전으로 되어 있는 경우  - 제 경우는 오늘 오전에 마지막 빌드 후 15.9.10로 변경했었음.

빌드 시에 하기 커맨드가 무한으로 불리면서 빌드가 안 되는 현상이 있습니다.

 

C:\Qt\Qt5.4.1\5.4\mingw491_32\bin\qmake.exe -spec win32-g++ -o Makefile smartlog2.pro


시스템 시간을 최근 혹은 오늘로 변경하면 문제 없이 빌드가 됩니다.

by 무위자연 2015. 11. 17. 14:52

C / C++에서 float 형으로 저장된 값을 메모리에 쓰고

그 메모리있는 hex 값을 읽어서 qt에서 float으로 원복하는 방법.


1. 각 메로리에 있는 hex 값을 읽어온다.


QByteArray temparr;
            temparr.append(((rcvPacket[1] & 0x0F)*16 + (rcvPacket[2] & 0x0F)));
            temparr.append(((rcvPacket[4] & 0x0F)*16 + (rcvPacket[5] & 0x0F)));
            temparr.append(((rcvPacket[7] & 0x0F)*16 + (rcvPacket[8] & 0x0F)));

            temparr.append(((rcvPacket[10] & 0x0F)*16 + (rcvPacket[11] & 0x0F)));

2. 읽어온 데이터를 역으로 읽어서 계산한다.


float gainslope = bytesToFloat(temparr[3], temparr[2], temparr[1], temparr[0])

float SerialProtocol3::bytesToFloat(uchar b0, uchar b1, uchar b2, uchar b3)
{
    float output;

    *((uchar*)(&output) + 3) = b0;
    *((uchar*)(&output) + 2) = b1;
    *((uchar*)(&output) + 1) = b2;
    *((uchar*)(&output) + 0) = b3;

    return output;
}

장치데이터 읽은 쪽에서 유용한다.


by 무위자연 2015. 10. 28. 16:46
| 1 2 3 |