관련 문서 doc.qt.io/qt-5.14/qsqlquery.html#last

 

QSqlQuery Class | Qt SQL 5.14.2

QSqlQuery Class The QSqlQuery class provides a means of executing and manipulating SQL statements. More... Header: #include qmake: QT += sql Public Types Public Functions Detailed Description QSqlQuery encapsulates the functionality involved in creating, n

doc.qt.io

QSQlQuery는 결과를 return하면 해당 결과는 테이블 형태로 반환하게 된다.

간단하게 select해서 count를 보려면 다음과 같이 처리한다.

 
QSqlQuery fileQuery(LocalDB);
QString queryString = QString("SELECT count(*) FROM file_export where cid = %1 and ccid = %2 and programid = %3")
.arg(caseID).arg(caseItemID).arg(static_cast<int>(enumValue));
 
if (fileQuery.exec(queryString) == false)
return metaDataString;
 
int numberOfRows = 0;
if (fileQuery.next()) {//한번은 next를 해줘야 1번째 결과 행으로 이동한다.
numberOfRows = fileQuery.value(0).toInt();
}
else {
return metaDataString;
}
 

이 쿼리를 db 프로그램에서 select하면 다음과 같이 나오고 0번째가 그 값이 되기 때문이다.

by 무위자연 2020. 9. 23. 14:18

QTableView의 ItemDelegate에서 해당 Row가 mouse hover 상태인지 직접 알수가 없다.


그래서 다음의 단계가 필요하다.


1. QTableView를 상속 받은 클래스를 만들고 해당 클래스는 다음과 같이 mousemove, mouse leaver에 대한 event를 처리하게 한다.

CustomTableView::CustomTableView(QWidget *parent) : QTableView(parent)

{

setMouseTracking(true);

}

void CustomTableView::mouseMoveEvent(QMouseEvent *event)

{

QModelIndex index = indexAt(event->pos());

emit hoverIndexChanged(index);

}

void CustomTableView::leaveEvent(QEvent *event)

{

emit leaveTableEvent();

viewport()->update();

}


2. 이 때 발생한 signal을 처리할 수 있도록 이 CustomTableView를 사용하는 곳에서 signal / slot을 설정한다.

connect(customTable_,

&ui::CustomTableView::hoverIndexChanged,

itemDelegate,

&ui::LoginInfoTableDelegate::slotHoverIndexChanged);

connect(loginInfoTable_,

&ui::CustomTableView::leaveTableEvent,

itemDelegate,

&ui::LoginInfoTableDelegate::slotLeaveTableEvent);


3. 사용할 ItemDelegate에서 다음과 같이 처리한다.

void CustomTableDelegate::slotHoverIndexChanged(const QModelIndex& item)

{

hoverRow_ = item.row();

}


void CustomTableDelegate::slotLeaveTableEvent()

{

hoverRow_ = -1;

}


4. 3번 slot으로 인해서 ItemDelegate의 Paint에서는 hoverRow_를 알 수 있게 된다.

void CustomTableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const

{

int column = index.column();

QStyleOptionViewItem itemOption(option);

bool isHovered = false;

if (index.row() == hoverRow_)

{

itemOption.state = itemOption.state | QStyle::State_MouseOver;

isHovered = true;

}

else

{

itemOption.state = itemOption.state &  (~QStyle::State_MouseOver);

}


이제 마음껏 hover 되었을 때의 효과를 반영하면 된다~



by 무위자연 2019. 3. 13. 13:48


참고 페이지 : https://doc.qt.io/Qt-5/qtprintsupport-index.html / 

https://wiki.qt.io/Exporting_a_document_to_PDF  


#1 QTextDocument 를 생성하고 html 코드를 넣어준다.

QTextDocument doc;

doc.setHtml("<h1>Hello, World!2222</h1>\n<p>Lorem ipsum dolor sit amet, consectitur adipisci elit</p>");
//doc.setHtml(docString);

doc.setPageSize(printer.pageRect().size()); // This is necessary if you want to hide the page number


#2-1 생성한 doc를 가지고 pdf 저장하기.

QPrinter printer(QPrinter::PrinterResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4);
printer.setOutputFileName(fileName);

doc.print(&printer);


#2-2 생성한 doc를 가지고 인쇄하기

QPrinter printer;
QPrintDialog popup(&printer);
if (popup.exec() == QDialog::Accepted) //인쇄 설정창에서 확인을 하면
{
    doc.print(&printer); //인쇄를 한다.
}




by 무위자연 2019. 2. 20. 15:25

circle 안에 이미지가 들어가는 형태를 구현하기가 생각보다 어렵고.


고정된 이미지일 경우 style sheet로 간단하게 구현이 된다.


하지만 로컬 이미지를 가지고? circle 형태의 QLabel을 만들기는 까다롭다



그래서 QLabel를 상속 받아서 다음과 같이 처리하면 쉽게? 그릴수 있다.

CircleLabel은 QLabel을 상속 받은 QWidget이다.

private:

void paintEvent(QPaintEvent *event) override;//paintEvent을 override한다


void CircleLabel::paintEvent(QPaintEvent *event)

{

QString pixmapPath = imagePath_;

if (imagePath_.isEmpty())

{

pixmapPath = ":image/defaultimage.png";

}

//draw main image

QPixmap mainPixmap(pixmapPath);

QPixmap scaled = mainPixmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding,                 Qt::SmoothTransformation);

QBrush brush(scaled);

QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing, true);

painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

painter.setBrush(brush);

painter.setPen(QColor("red"));//pen 색을 적절히 지정해주면 antialiasing이 부족한 부분이 티가 덜 난다~

painter.drawRoundedRect(0, 0, width(), height(), 90, 90);

//만일 2개 이미지를 겹쳐서 그린 다면 다음과 같이 추가해주면 된다.- 첨부이미지 기준으로 스머프 우측 하단에 //설정 이미지와 같은.~

QPixmap settingPixmap(":image/Edit.png");

painter.drawPixmap(66, 66, settingPixmap);


QLabel::paintEvent(event);

}

by 무위자연 2018. 12. 10. 11:40

Completer가 보이는 UI를 custom할 수 있다.

그래서 준비한 2탄

treeview도 가능하고

table view도 가능하다.

QStringListModel *dateModel = new QStringListModel(stringList);


#1 completer를 표시할 UI를 생성한다. 예제는 tableview

QTableView* tableView = new QTableView(dateModel);
patientViewModel->setParent(tableView);
tableView->horizontalHeader()->setVisible(false);
tableView->setShowGrid(false);

dateCompleter->setPopup(tableView);//


그리고 itemdelegate를 set해서 tableview를 더 풍성하게 보이고 싶다면...


#2 itemDelegate를 생성한다.

이 생성한 itemdelegate를 tableview에 넣어도 반영되지 않는다. 다음과 같이  completer의 popup에 바로 set해줘야 한다.!!!
dateCompleter->popup()->setItemDelegate(itemDelegate);


#3 model을 생성해서 임의로 만들고 싶다면?

QStandardItemModel *completerModel = new QStandardItemModel();

index = 0;

for (const auto& curRecord : records)
{
    QString name = list[index].name;
    QString genderString = list[index].gender
    QString birth = list[index].birth;
    completerModel->setItem(index, 0, new QStandardItem(name));
    completerModel->setItem(index, 1, new QStandardItem(genderString));
    completerModel->setItem(index, 2, new QStandardItem(birth));
    index++;
} 
return completerModel;
}


by 무위자연 2018. 12. 6. 17:33

QTableView::item

{

background-color : bg_white;

margin-bottom: 10px;

border-bottom : 1px solid contents_outer;

border-top : 1px solid contents_outer;

outline:0px;

}


이런 식의 정보가 아니다!

tableView->verticalHeader()->setDefaultSectionSize(120);

by 무위자연 2018. 11. 29. 18:21

관련 에러 메시지 : 

QObject::connect No such slot QLineEdit::slotUpdateClearButton(const QString&)


해당 클래스는 QLineEdit을 상속 받은 custom 클래스다

다음과 같이 connect했으나 에러가 발생함.


connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotUpdateClearButton(const QString&)));


해당 메시지는 slot이 없다는 것으로 오해 할 수 있다.


하지만 이건 QObject가 그런(?) slot과 연결할 수 없다는 의미이다.


즉 비록 QLineEdit을 상속 받았지만 해당 custom class 존재를 QObject가 모르겠다는 의미이다.


이 경우 

class CustomWidget : public QLineEdit

{

Q_OBJECT//이거를 넣어줘야 한다!!!!

public:

explicit CustomWidget (bool enableClearBtn = false, QWidget* parent = nullptr);

virtual ~CustomWidget ();




by 무위자연 2018. 11. 20. 15:59


just simple


example function : drawFooterArea(QPainter *painter, const QRect& rect, const QSqlRecord& record) const


1 set pen / brush the color you want

painter->setPen(Qt::red);

painter->setBrush(Qt::red);


2. draw Ellipse

painter->drawEllipse(rect);


if you don't set brush, just show circle line


by 무위자연 2018. 9. 27. 10:36

enum 정의를 해서 사용하는데

그 정의가 늘어날 때마다 관련 코드를 넣는 것은 매우 고통스러운 일이다.

그리고 enum 실제 값과 해당하는 enum을 찾아서 처리해야하는 일들이 생긴다.

그 럴 때 사용하는 것이  QMetaEnum( 관련 문서 : http://doc.qt.io/qt-5/qmetaenum.html )이다.

예제.

class Currency : public QObject
{
Q_OBJECT
public:
enum class CurrencyType
{
    ABC = 1,
    CDE = 2,
    DEF = 3,
   _count,//이것이 있어야 함!!!
}
Q_ENUM(CurrencyType)

QObject를 상속 받아서 정의하고 _count를 넣은 다음에 Q_ENUM으로 정의해주어야 한다.
enum 값을 int로만 사용하면 static_cast<int>를 이용하면 충분하지만 enum의 이름을 사용할 필요가 있을 때 사용한다.

#1 QString to enum
QString 값을 enum value로 바꿔준다.
auto currencyMap = QMetaEnum::fromType<Currency::CurrencyType>(); //enum 클래스를 가지는 객체를 만든다.
QString currency = "ABC";
for (qint32 i = 0; i < currencyMap.keyCount(); i++)
{
    if (currencyMap.key(i) == currency)//key 값과  QString을 비교한다.
   {
                return Currency::CurrencyType(i); 
     }
}


#2 enum to QString

Currency::CurrencyType tempType = currency;

QString currencyString = QVariant::fromValue(currency).value<QString>();


by 무위자연 2018. 9. 11. 10:48


#1 QObject를 상속 받은 tooltip을 처리할 class를 선언한다


at ZenoToolTipHelper.h

class ZenoToolTipHelper : public QObject

{

Q_OBJECT

public:

explicit ZenoToolTipHelper(QObject* parent = NULL)

: QObject(parent)

{

}


protected:

bool eventFilter(QObject* obj, QEvent* event);

};


at ZenoToolTipHelper.cpp

#include <QAbstractItemView>

#include <QToolTip>

#include <QEvent>

#include <QHelpEvent>

bool ZenoToolTipHelper::eventFilter(QObject* obj, QEvent* event)

{

if (event->type() == QEvent::ToolTip)

{

QAbstractItemView* view = qobject_cast<QAbstractItemView*>(obj->parent());

if (view == nullptr)

{

return false;

}


QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);

QPoint pos = helpEvent->pos();

QModelIndex index = view->indexAt(pos);

if (index.isValid() == false)

return false;

//row , column을 적절하게 판단하여서 tooltip을 보여준다.

QFontMetrics fm(view->font());

QRect rect = view->visualRect(index);

int rectWidth = rect.width();


QToolTip::showText(helpEvent->globalPos(),               "tooltip 내용들", view, rect);

return true;


}

return false;

}


#2 #1에서 만들 class를 tableView에 set한다.

tableView->viewport()->installEventFilter(new ZenoToolTipHelper());

by 무위자연 2018. 7. 11. 15:06
| 1 2 3 |