国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Qt 知識點收集(一)

2019-11-08 19:34:56
字體:
來源:轉載
供稿:網友
掛件:QLabelQLineEditQTabWidgetQTableWidgetQGroupBoxQCheckBoxQRadioBotton QAbstractBottonQPushBotton布局QHBoxLayout horizontalQVBoxLayout vertivalQFont font ( "Microsoft YaHei", 10, 75);//第一個屬性是字體(微軟雅黑),第二個是大小,第三個是加粗(權重是75)ui->label->setFont(font);常見權重QFont::Light - 25 高亮QFont::Normal - 50 正常QFont::DemiBold - 63 半粗體QFont::Bold - 75 粗體QFont::Black - 87 黑體QFont font1("Fantasy",24);//設置字體格式以及大小label1->setFont(font1);label1->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//設置居中label1->setText(tr("點擊↓開始"));//設置文字QGridLayout *layout = new QGridLayout(this);layout->addWidget(label1, 0 ,1);//布局加部件layout->addWidget(btn1, 1, 1);layout->setSizeConstraint(QLayout::SetFixedSize);//設置合適尺寸QHBoxLayout *Leftlayout = new QHBoxLayout;//設置0,1兩列的所占比例Leftlayout->setColumnStretch(0,1);Leftlayout->setColumnStretch(0,3);//設置占幾行,占幾列Leftlayout->addWidget(OtherLabel,5,0,1,2);//設置固定大小Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); this->setMaximumSize(393,171); this->setMinimumSize(393,171);}//設置圖片LabelHeadLabel = new QLabel(tr("頭像"));HeadIconLabel = new QLabel;QPixmap icon("1.png");HeadIconLabel->setPixmap(icon);//label顯示圖像HeadIconLabel->resize(icon.width(),icon.height());//設置lable和頭像一些養大//設置間距ToPRightLayout = new QHBoxLayout();TopRightLayout->setSpacing(20);//設置邊距TopRightLayout = new QHBoxLayout();TopRightLayout->setMargin(10);//設置占位符ButtonLayout = new QHBoxLayout();ButtonLayout->setStretch();ButtonLayout->addWidget(OkBtn);//設置文件打開對話窗口QString s = QFileDialog::getOpenFileName(this, "open file", "c:","C++ files(*.cpp);;C files(*.c);;Head files(*.h)");if(!s.isEmpty) setWindowTitle(s);//設置消息對話框if( QMessageBox::question(this, "問題","有個問題要問你",QMessageBox::Ok | QMessageBox::Cancle,QMessageBox::Ok) == QMessageBox::Ok ){}else{}toolBtn1=new QToolButton();toolBtn1->setText("張三");//設置QToolButton按鈕標題toolBtn1->setIcon(QPixmap("1.png"));//設置QToolButton的圖像toolBtn1->setIconSize(QPixmap("1.png").size());//設置QToolButton的大小和圖像一致toolBtn1->setAutoRaise(true);//設置QToolButton按鈕自動彈起toolBtn1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);//設置文字在圖片的旁邊#include <QFile>#include <QTextStream>//設置文件打開對話窗口QString file = QFileDialog::getOpenFileName();QFile file(filename);if(file.open(QFile::ReadOnly)){ QTextStream stream(&file); while(!stream.atEnd()) { s = stream.readLine(); QMessageBox::information(this, "文件內容", s); } file.close();}//設置menubarMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ open = new QAction(tr("打開"),this); open->setShortcut(tr("Ctrl+O"));//設置快捷鍵 fileexit = new QAction(tr("退出"),this); fileexit->setShortcut(tr("Ctrl+E")); menu = menuBar()->addMenu(tr("文件")); menu->addAction(open); menu->addSeparator(); menu->addAction(fileexit); menu = menuBar()->addMenu(tr("About")); edit1 = new QTextEdit; setCentralWidget(edit1); connect(open, SIGNAL(triggered()), this, SLOT(openfile()));}//關閉事件void MainWindow::closeEvent(QCloseEvent *event){ QMessageBox::StandardButton button = QMessageBox::question(this, tr("退出程序"),QString(tr("是否退出")), QMessageBox::Yes|QMessageBox::No,QMessageBox::No); if(button == QMessageBox::Yes) { event->accept(); } else { event->ignore(); }}//設置textedit光標位置QTextCursor cursor = textEdit->textCursor();cursor.movePosition(QTextCursor::Start);textEdit->setTextCursor(cursor);//獲取host信息QT如果要進行網絡編程首先需要在.pro中添加如下代碼:QT += network在頭文件中包含相關頭文件#include <QHostInfo>#include <QNetworkInterface>void Widget::btn_click(){ //獲取主機名稱 QString s=QHostInfo::localHostName(); //根據主機名獲取主機其他信息 QHostInfo info=QHostInfo::fromName(s); /*獲取主機所有的網絡地址,ip地址跟網卡有關系,一個主機可能存在多個網卡或者虛擬機網卡*/ QList<QHostAddress> list=info.addresses(); if(!list.isEmpty()) { /*設置一個迭代器*/ QList<QHostAddress>::iterator i; for(i=list.begin();i!=list.end();i++) { QMessageBox::information(this,"主機地址",(*i).toString()); } } label1->setText(s);}void Widget::btn_click(){ QString detail; /*得到本機所有的網絡接口信息*/ QList<QNetworkInterface> list=QNetworkInterface::allInterfaces(); QList<QNetworkInterface>::iterator i; for(i=list.begin();i!=list.end();i++) { QNetworkInterface interface=*i; /*獲取設備名字*/ detail=tr("設備:")+interface.name()+"/n"; detail+=tr("硬件地址:")+interface.hardwareAddress()+"/n"; QList<QNetworkAddressEntry> entrylist=interface.addressEntries(); QList<QNetworkAddressEntry>::iterator j; for(j=entrylist.begin();j!=entrylist.end();j++) { // QNetworkAddressEntry entry=*j; detail+="/t"+tr("IP地址:")+entry.ip().toString()+"/n"; detail+="/t"+tr("子網掩碼:")+entry.netmask().toString()+"/n"; detail+="/t"+tr("廣播地址:")+entry.broadcast().toString()+"/n"; } QMessageBox::information(this,"主機信息",detail); }}//重定向cmdWidget::Widget(QWidget *parent) : QWidget(parent){ QString cmd; cmd = edit1->text(); QProcess p2(0); p2.start("cmd", QStringList()<<"/c"<<cmd); p2.waitForStarted(); p2.waitForFinished(); QString strTemp=QString::fromLocal8Bit(p2.readAllStandardOutput()); label1 = new QLabel(this); label1->setText(strTemp);}//設置label顏色 QPalette pe; pe.setColor(QPallette::WindowText,Qt::red); label0->setPalette(pe); label0->setText(tr("請輸入命令"));//設置dialog背景圖片this->setAutoFillBackground(true);QPalette palette;palette.setBrush(QPalette::Background, QBrush(QPixmap("2.jpg")));setPalette(palette);//設置scrollAreascrollArea = new QScrollArea;scrollArea->setBackgroundRole(QPalette::Base);scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);scrollArea->setWidget(recv);//設置UTF8QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8")); TCP-client://連接serverip=new QHostAddress();serverip->setAddress(ipaddr);tcpsocket->connectToHost(*serverip,port.toInt());//發送QString strtext=edit3->text();if(!strtext.isEmpty()) tcpsocket->write(strtext.toStdString().data());//接收消息void Widget::myrecvdata(){ char buf[1024]={0}; /*bytesAvailable()表示有效數據*/ while(tcpsocket->bytesAvailable()>0) { memset(buf,0,sizeof(buf)); tcpsocket->read(buf,sizeof(buf)); textb->append(buf); //QMessageBox::information(this,"消息",buf); }}//設置最大化最小化按鈕無效setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint & ~Qt::WindowMinimizeButtonHint);//設置不在任務欄出現setWindowFlags(windowFlags() | Qt::Tool);////隱藏最大化按鈕 this->setWindowFlags(this->windowFlags()&~Qt::WindowMaximizeButtonHint); //去掉問號Qt::WindowFlags flags=Qt::Dialog; flags |=Qt::WindowCloseButtonHint; setWindowFlags(flags);//當前時間 QString curTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");//設置文件過濾器void MainWindow::savefile(){ QFileDialog dialog(this,"保存文件","C://Users//Administrator//Desktop" ,tr("富文本(*.rtf);;純文本(*.txt)")); QString filterMod; QString fileName; if(dialog.exec()) { fileName = dialog.selectedFiles()[0]; filterMod = dialog.selectedNameFilter(); } QFile file(fileName); if(file.open(QFile::WriteOnly |QFile::Truncate)) { QTextStream stream(&file); if("富文本(*.rtf)" == filterMod) stream<<textEdit->toHtml(); if("純文本(*.txt)" == filterMod) stream<<textEdit->toPlainText(); } file.close();}//設置顏色字體void MainWindow::colslot(){ const QColorDialog::ColorDialogOptions options = QFlag(QColorDialog::ShowAlphaChannel); const QColor color = QColorDialog::getColor(Qt::green, this, "Select Color", options); if (color.isValid()) { textEdit->setTextColor(color); }}void MainWindow::fontslot(){ bool ok; QFont font = QFontDialog::getFont( &ok, QFont("Helvetica [Cronyx]", 10), this); if (ok) { textEdit->setFont(font); } else { ; }}//設置光標的選區,使格式作用于選區內的字符void MainWindow::mergeFormat(QTextCharFormat fmt){ QTextCursor cursor = textEdit->textCursor(); if (!cursor.hasSelection()) { cursor.select(QTextCursor::WordUnderCursor); } cursor.mergeCharFormat(fmt);}//設置分隔字符串QString mainPage = "mainpage=http://www.baidu.com"QStringList list = mainPage.split("=");//捕獲消息void event(QEvent* ev);bool MyWidget::event(QEvent* ev){ //消息被截斷 if(ev.type == QEvent::MouseButtonPress) return true; //如果不是鼠標消息,就繼續傳遞消息 return QWidget::event(ev);}//重載具體的虛函數,處理鼠標事件#include <QMouseEvent>void mousePressEvent(QMouseEvent* ev);void mouseReleaseEvent(QMouseEvent* ev);void mouseMoveEvent(QMouseEvent* ev);bool MyWidget::mousePressEvent(QMouseEvent* ev)//重載{ QPoint pt = ev->pos(); qDebug()<< pt; if(ev->button() == Qt::LeftButton) { .... } //shift被按下 if(ev->modifiers() == Qt::ShiftModifier) { .... }}//鼠標消息,鍵盤消息#include <QKeyEvent>void keyPressEvent(QMouseEvent* ev);{ ev->modifiers(); int key = ev->key(); qDebug() << (char)key; }void keyReleaseEvent(QMouseEvent* ev);//鼠標不需要按下,就得到消息this->setMousetTracking(true);//傳遞消息第一種方式,消息過濾器MyWidget::MyWidget(QWidget *parent) : QWidget(parent){ QPushButton* button; button = new QPushButton("This button", this); connect(button, SIGNAL(clicked()), this, SLOT(close())); _button = button; /* button給自己安裝了一個消息過濾器,那么經過button的消息,都先要調用它的過濾器的eventFilter函數 */ button->installEventFilter(this);}bool MyWidget::eventFilter(QObject *o, QEvent *e){#if 0 if(o == (QObject*)_button &&( e->type() == QEvent::MouseButtonRelease || e->type() == QEvent::MouseButtonDblClick || e->type() == QEvent::MouseButtonPress)) { return true; }#endif return QWidget::eventFilter(o, e);}//傳遞消息第二種方式,App消息通知bool Myapplication::notify(QObject *o, QEvent *e){ if(this->topLevelWidgets().count()>0) { QWidget* mainWnd = this->topLevelWidgets().at(0); if(o==(QObject*)mainWnd && e->type() == QEvent::MouseButtonPress) { // do ... qDebug() << "mainwnd is clicked"; } } return QApplication::notify(o, e);}//傳遞消息第三種方式,自定義消息bool MyWidget::event(QEvent *e){ if(e->type() == QEvent::User) { qDebug() << "User event is comming"; } return QWidget::event(e);}int main(int argc, char* argv[]){ MyApplication app(argc, argv); MyWidget w; w.show(); // 發送一個Event給MyWidget qDebug() << "begin send"; app.postEvent(&w, new QEvent(QEvent::User)); qDebug() << "end send"; // app.sendEvent(&w, ) return app.exec();}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香港 | 阳山县| 沂南县| 津市市| 合江县| 英吉沙县| 高邮市| 昆明市| 油尖旺区| 梁山县| 健康| 南京市| 临夏县| 兴化市| 宝坻区| 凉城县| 赤峰市| 清河县| 江达县| 东乡县| 桓仁| 青阳县| 武鸣县| 岳普湖县| 仪陇县| 南宫市| 阿拉善盟| 安西县| 平山县| 民勤县| 库伦旗| 迁西县| 仲巴县| 巴林左旗| 布尔津县| 开江县| 定安县| 历史| 南汇区| 铁岭市| 安义县|