Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿

使用QT Designer设计软件界面
使用过程没什么说的

假定生成的ui文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 
<class>Form</class>
 
<widget class="QWidget" name="Form">
  
<property name="geometry">
   
<rect>
    
<x>0</x>
    
<y>0</y>
    
<width>203</width>
    
<height>177</height>
   
</rect>
  
</property>
  
<property name="windowTitle">
   
<string>Form</string>
  
</property>
  
<widget class="QWidget" name="">
   
<property name="geometry">
    
<rect>
     
<x>20</x>
     
<y>20</y>
     
<width>164</width>
     
<height>139</height>
    
</rect>
   
</property>
   
<layout class="QVBoxLayout" name="verticalLayout_3">
    
<item>
     
<widget class="QLineEdit" name="lineEdit"/>
    
</item>
    
<item>
     
<layout class="QHBoxLayout" name="horizontalLayout">
      
<item>
       
<layout class="QVBoxLayout" name="verticalLayout_2">
        
<item>
         
<widget class="QPushButton" name="pushButton_7">
          
<property name="text">
           
<string>PushButton</string>
          
</property>
         
</widget>
        
</item>
        
<item>
         
<widget class="QPushButton" name="pushButton_5">
          
<property name="text">
           
<string>PushButton</string>
          
</property>
         
</widget>
        
</item>
        
<item>
         
<widget class="QPushButton" name="pushButton">
          
<property name="text">
           
<string>PushButton</string>
          
</property>
         
</widget>
        
</item>
       
</layout>
      
</item>
      
<item>
       
<layout class="QVBoxLayout" name="verticalLayout">
        
<item>
         
<widget class="QPushButton" name="pushButton_2">
          
<property name="text">
           
<string>PushButton</string>
          
</property>
         
</widget>
        
</item>
        
<item>
         
<widget class="QPushButton" name="pushButton_8">
          
<property name="text">
           
<string>PushButton</string>
          
</property>
         
</widget>
        
</item>
        
<item>
         
<widget class="QPushButton" name="pushButton_4">
          
<property name="text">
           
<string>PushButton</string>
          
</property>
         
</widget>
        
</item>
       
</layout>
      
</item>
     
</layout>
    
</item>
   
</layout>
  
</widget>
 
</widget>
 
<resources/>
 
<connections/>
</ui>
然后修改pro文件
加入对QUitools的链接
CONFIG += uitools
其main.cpp如下:
#include <QFile>
#include 
<QtGui/QApplication>
#include 
<QtGui/QWidget>
#include 
<QtGui/QLineEdit>
#include 
<QtUiTools/QuiLoader>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QUiLoader ui;
    QFile file(
"dlg.ui");
    QWidget
* widget = ui.load(&file);
    
if(widget)
    {
        widget
->setWindowTitle("Dlg");
        QLineEdit
* lineedit = widget->findChild<QLineEdit*>("lineEdit");
        lineedit
->setStyleSheet("QLineEdit {background-color: yellow}");
        lineedit
->setText("information.");
        widget
->show();
    }
    
return a.exec();
}
posted @ 2011-08-05 22:01 ccsdu2009 阅读(935) | 评论 (0)编辑 收藏
 
#include <QtGui/QApplication>
#include 
<QtGui/QScrollArea>
#include 
"widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget 
*widget = new Widget;

    QScrollArea area;
    area.setWidget(widget);
    area.viewport()
->setBackgroundRole(QPalette::Dark);
    area.viewport()
->setAutoFillBackground(true);
    area.setWindowTitle(
"Icon Cool");
    area.show();

    
return a.exec();
}
posted @ 2011-08-03 22:37 ccsdu2009 阅读(460) | 评论 (0)编辑 收藏
 
#include <QColor>
#include 
<QImage>
#include 
<QtGui/QWidget>

class Widget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QColor penColor READ penColor WRITE setPenColor)
    Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage)
    Q_PROPERTY(
int zoomFactor READ zoomFactor WRITE setZoomFactor)
public:
    Widget(QWidget 
*parent = 0);
    
~Widget();

    
void   setPenColor(const QColor& newColor);
    QColor penColor()
const{return curColor;}
    
void   setZoomFactor(int newZoom);
    
int    zoomFactor()const{return zoom;}
    
void   setIconImage(const QImage& image);
    QImage iconImage()
const{return image;}
    QSize  sizeHint()
const;
protected:
    
void   mousePressEvent(QMouseEvent *event);
    
void   mouseMoveEvent(QMouseEvent* event);
    
void   paintEvent(QPaintEvent* event);
private:
    
void   setImagePixel(const QPoint &pos,bool opaque);
    QRect  pixelRect(
int i,int j)const;

    QColor curColor;
    QImage image;
    
int    zoom;
};

#include <QtGui>
#include "widget.h"
Widget::Widget(QWidget *parent)
 : QWidget(parent)
{
 this->setAttribute(Qt::WA_StaticContents);
 this->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
 this->curColor = Qt::black;
 this->zoom = 8;
 image = QImage(16,16,QImage::Format_ARGB32);
 image.fill(qRgba(0,0,0,0));
}
Widget::~Widget()
{ 
}
QSize Widget::sizeHint()const
{
 QSize size = zoom * image.size();
 if(zoom >= 3)
 size += QSize(1,1);
 return size;
}
void Widget::setPenColor(const QColor& newColor)
{
 curColor = newColor;
}
void Widget::setIconImage(const QImage& image)
{
 if(this->image != image)
 {
 this->image = image.convertToFormat(QImage::Format_ARGB32);
 update();
 this->updateGeometry();
 }
}
void Widget::paintEvent(QPaintEvent* event)
{
 QPainter painter(this);
 if(zoom >=3)
 {
 painter.setPen(this->palette().foreground().color());
 for(int i = 0; i <= image.width();++i)
 painter.drawLine(zoom*i,0,zoom*i,zoom*image.height());
 for(int j = 0;j <= image.height();j++)
 painter.drawLine(0,zoom*j,zoom*image.width(),zoom*j);
 }
 for(int i = 0; i < image.width();i++)
 {
 for(int j = 0;j < image.height();j++)
 {
 QRect rect = this->pixelRect(i,j);
 if(!event->region().intersect(rect).isEmpty())
 {
 QColor color = QColor::fromRgba(image.pixel(i,j));
 if(color.alpha() < 255)
 painter.fillRect(rect,Qt::white);
 painter.fillRect(rect,color);
 }
 }
 }
}
QRect Widget::pixelRect(int i, int j) const
{
 if(zoom >=3)
 return QRect(zoom*i+1,zoom*j+1,zoom-1,zoom-1);
 return QRect(zoom*i,zoom*j,zoom,zoom);
}
void Widget::mousePressEvent(QMouseEvent *event)
{
 if(event->button() == Qt::LeftButton)
 setImagePixel(event->pos(),true);
 else if(event->button() == Qt::RightButton)
 setImagePixel(event->pos(),false);
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
 if(event->buttons() & Qt::LeftButton)
 setImagePixel(event->pos(),true);
 else if(event->buttons() & Qt::RightButton)
 setImagePixel(event->pos(),false);
}
void Widget::setImagePixel(const QPoint &pos, bool opaque)
{
 int i = pos.x()/zoom;
 int j = pos.y()/zoom;
 if(image.rect().contains(i,j))
 {
 if(opaque)
 image.setPixel(i,j,penColor().rgba());
 else
 image.setPixel(i,j,qRgba(0,0,0,0));
 this->update(this->pixelRect(i,j));
 }
}
void Widget::setZoomFactor(int newZoom)
{
 if(newZoom < 1)
 newZoom = 1;
 if(newZoom != zoom)
 {
 zoom = newZoom;
 update();
 updateGeometry();
 }
}
几点说明:
1.宏 Q_PROPERTY 声明属性读写属性
2.setSizePolicy(.,.) 说明控件最小尺寸为推荐尺寸
3.sizeHint() 获得控件推荐尺寸
4.控件 update()/updateGeometry()用于下次重绘和调整控件尺寸
5.在函数paintEvent中绘制图元
6.一个控件的调色板有3个颜色组 激活,非激活,不可用
posted @ 2011-08-03 22:15 ccsdu2009 阅读(1190) | 评论 (0)编辑 收藏
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    edit 
= new QTextEdit;
    
this->setCentralWidget(edit);
    setAcceptDrops(
true);
    
this->setWindowTitle(tr("Text Editor"));
}

MainWindow::
~MainWindow()
{
}

void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
    
if(event->mimeData()->hasFormat("text/uri-list"))
        
event->acceptProposedAction();
}

void MainWindow::dropEvent(QDropEvent* event)
{
    QList
<QUrl> uri = event->mimeData()->urls();
    
if(uri.isEmpty())
        
return;
    QString file 
= uri.first().toLocalFile();
    
if(file.isEmpty())
        
return;
    
this->setWindowTitle(tr("%1 -- %2").arg(file).arg("Drag File"));
}
posted @ 2011-08-02 22:39 ccsdu2009 阅读(531) | 评论 (0)编辑 收藏
 
一个简单的使用例子如下:
#include "mainwindow.h"
#include 
<QtGui/QDockWidget>
#include 
<QtGui/QTextEdit>

MainWindow::MainWindow(QWidget 
*parent)
    : QMainWindow(parent)
{
    QDockWidget
* widget = new QDockWidget(tr("DockWidget"));
    QTextEdit
* edit = new QTextEdit;
    widget
->setWidget(edit);
    widget
->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    
this->addDockWidget(Qt::LeftDockWidgetArea,widget);
}

MainWindow::
~MainWindow()
{

}
需要说明的是仅MainWidow仅可设置Dock
posted @ 2011-08-02 22:16 ccsdu2009 阅读(777) | 评论 (0)编辑 收藏
 
#include <QtGui/QApplication>
#include 
<QtGui/QTextEdit>
#include 
<QtGui/QSplitter>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTextEdit
* text1 = NULL;
    QTextEdit
* text2 = NULL;
    QSplitter
* splitter;
    text1 
= new QTextEdit;
    text2 
= new QTextEdit;
    splitter 
= new QSplitter(Qt::Vertical);
    splitter
->addWidget(text1);
    QTextEdit
* text3 = new QTextEdit;
    QSplitter
* sp1 = new QSplitter(Qt::Horizontal);
    splitter
->addWidget(text2);
    splitter
->setStretchFactor(1,1);
    sp1
->addWidget(splitter);
    sp1
->addWidget(text3);
    sp1
->show();
    
return a.exec();
}
posted @ 2011-08-02 21:52 ccsdu2009 阅读(629) | 评论 (0)编辑 收藏
 
接上文
一个简单的基于QT的程序启动画面代码为:
#include <QtGui/QApplication>
#include 
<QtGui/QSplashScreen>
#include 
<QtGui/QPixmap>
#include 
"registerdlg.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSplashScreen
* splash = new QSplashScreen(QPixmap("DevCPP.bmp"));
    splash
->show();

    RegisterDlg w;

    
//! load some data

    Qt::Alignment topright 
= Qt::AlignRight | Qt::AlignTop;
    splash
->showMessage(QObject::tr("Loading addins"),topright,Qt::blue);

    
//! load soma data

    
for(int i = 0;i< 9999;i++)
        ;

    w.show();
    splash
->finish(&w);
    delete splash;
    
return a.exec();
}
posted @ 2011-08-01 22:16 ccsdu2009 阅读(495) | 评论 (0)编辑 收藏
 
还不太完善
#ifndef REGISTER_DLG_H
#define REGISTER_DLG_H

#include 
<QtGui/QDialog>
#include 
<QtGui/QVBoxLayout>
#include 
<QtGui/QHBoxLayout>
#include 
<QtGui/QGridLayout>
#include 
<QtGui/QLabel>
#include 
<QtGui/QLineEdit>
#include 
<QtGui/QPushButton>

class RegisterDlg : public QDialog
{
    Q_OBJECT
public:
    RegisterDlg(QWidget 
*parent = 0);
    
~RegisterDlg();
public slots:
    
void checkRegister();
private:
    QVBoxLayout
*  layout;
    QLabel
*       label;
    QGridLayout
*  gridlayout;
    QLabel
*       namelabel;
    QLineEdit
*    nameedit;
    QLabel
*       codelabel;
    QLineEdit
*    codeedit;
    QHBoxLayout
*  hlayout;
    QPushButton
*  confirm;
    QPushButton
*  cancel;
};

#endif
//! ccsdu2004
cxx
#include <QtGui/QMessageBox>
#include 
"registerdlg.h"

RegisterDlg::RegisterDlg(QWidget 
*parent)
    : QDialog(parent)
{
    layout 
= new QVBoxLayout;
    label  
= new QLabel(tr("Please input register info below"));
    layout
->addWidget(label);
    gridlayout 
= new QGridLayout;
    namelabel  
= new QLabel(tr("User Name"));
    nameedit   
= new QLineEdit();
    codelabel  
= new QLabel(tr("Register Code"));
    codeedit   
= new QLineEdit();
    gridlayout
->addWidget(namelabel,0,0);
    gridlayout
->addWidget(nameedit,0,1);
    gridlayout
->addWidget(codelabel,1,0);
    gridlayout
->addWidget(codeedit,1,1);
    layout
->addLayout(gridlayout);
    hlayout 
= new QHBoxLayout;
    confirm 
= new QPushButton(tr("Confirm"));
    cancel  
= new QPushButton(tr("Cancel"));
    hlayout
->addWidget(confirm);
    hlayout
->addWidget(cancel);
    layout
->addLayout(hlayout);
    
this->setLayout(layout);
    
this->setWindowTitle(tr("Register"));

    connect(
this->cancel,SIGNAL(clicked()),this,SLOT(close()));
    connect(
this->confirm,SIGNAL(clicked()),this,SLOT(checkRegister()));
}

RegisterDlg::
~RegisterDlg()
{
}

void RegisterDlg::checkRegister()
{
}
posted @ 2011-08-01 21:55 ccsdu2009 阅读(799) | 评论 (0)编辑 收藏
 
最近感觉都在瞎忙乎
无论是GUI还是Audio Effect都不能带来收益
学了几年编程
现在发现居然对UI编程还处于一年级水平
需要提高UI编程能力啊
这简直就是大BUG
posted @ 2011-07-31 22:16 ccsdu2009 阅读(468) | 评论 (1)编辑 收藏
 
int strncasecomp(char const *str1, char const *str2, unsigned long len)
{
    signed 
int c1 = 0, c2 = 0;

    
while (len--)
    {
        c1 
= tolower(*str1++);
        c2 
= tolower(*str2++);

        
if (c1 == 0 || c1 != c2)
            
break;
    }

    
return c1 - c2;
}
posted @ 2011-06-16 22:43 ccsdu2009 阅读(1428) | 评论 (0)编辑 收藏
仅列出标题
共38页: First 17 18 19 20 21 22 23 24 25 Last